Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda