'How to return the data in one function call for two different object keys

I have an object which looks like this :

const data = {
  students: [{
    code: '1',
    number: '22',
    type: 'regular',
    name: 'john',
    age: '11',
    class: 'A',
  }, {
    code: '2',
    number: '23',
    type: 'regular',
    name: 'steve',
    age: '12',
    class: 'B',
  }],
  teachers: [{
    code: '22',
    number: '101',
    type: 'intern',
    name: 'mac',
  }, {
    code: '23',
    number: '102',
    type: 'perm',
    name: 'jess',
  }],
};

It has different keys and values.

Here, I am trying to massage this data so that I can obtain the following result: So I am trying to get an array which will have only students data and other which will have teachers data from one function itself.

const result1 = [{
  code: '1',
  number: '22',
  type: 'regular',
  name: 'john',
}, {
  code: '2',
  number: '23',
  type: 'regular',
  name: 'steve',
}];
const result2 = [{
  code: '22',
  number: '101',
  type: 'intern',
  name: 'mac',
}, {
  code: '23',
  number: '102',
  type: 'perm',
  name: 'jess',
}];

what I tried is :

const getData = ({data = []}) => {
 data?.map({ code,
number, 
regular, 
name } ) => {
return{
code,
number, 
regular, 
name
}}
}

getData(data.students)
getData(data.teachers)   // How do get this data in one function call itself

This gives me the result , but for this I need to call this function twice once for students and one for teachers. I want to call this function once.

Thanks



Solution 1:[1]

Since from the provided data, it looks like the OP wants to mainly map a certain set of properties of student/teacher items from a map/object, a possible approach is to reduce the data object's entries and apply the mapping exclusively for a matching students or teachers key each referring to a valid array-type value.

const data = {
  students: [{ code: "1", number: "22", type: "regular", name: "john", age: "11", class: "A" }, { code: "2", number: "23", type: "regular", name: "steve", age: "12", class: "B" }],
  teachers: [{ code: "22", number: "101", type: "intern", name: "mac" }, { code: "23", number: "102", type: "perm", name: "jess" }],
};

const {

  students: result1,
  teachers: result2,

} = Object
  .entries(data)
  .reduce((result, [key, value]) => {
    if (
      // process only for a matching key ...
      (/^students|teachers$/).test(key)

      // ... and a valid value type.
      && Array.isArray(value)
    ) {
      result[key] = value
        .map(({ code, number, type, name }) =>
          ({ code, number, type, name })
        );
    }
    return result
  }, {});

console.log({ result1, result2 });
.as-console-wrapper { min-height: 100%!important; top: 0; }

Solution 2:[2]

I'm not sure what use there is in this, but since you want to run a function twice and get two results, do that, and combine into an object:

const data = {
  students: [{
    code: '1',
    number: '22',
    type: 'regular',
    name: 'john',
    age: '11',
    class: 'A'
  }, {
    code: '2',
    number: '23',
    type: 'regular',
    name: 'steve',
    age: '12',
    class: 'B'
  }],
  teachers: [{
    code: '22',
    number: '101',
    type: 'intern',
    name: 'mac'
  }, {
    code: '23',
    number: '102',
    type: 'perm',
    name: 'jess'
  }]
};
const transformData = (data = []) =>
  data.map(({
    code,
    number,
    regular,
    name
  }) => ({
    code,
    number,
    regular,
    name
  }));
const getData = (data) =>
  ({
    students: transformData(data.students),
    teachers: transformData(data.teachers)
  });
console.log(getData(data));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Note: I modified the transformData function to remove some extra syntax, and to remove the optional chaining since the Stack Snippets' ancient version of Babel doesn't support it.

Also, there's no property named regular on the original objects in the array, so they come out undefined.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2