Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda