Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could map new entries from the object and take the mapped new structure.

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' }] },
    getData = ({ code, number, regular, name }) => ({ code, number, regular, name }),
    result = Object.fromEntries(Object
        .entries(data)
        .map(([k, v]) => [k, v.map(getData)])
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

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; }

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!