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

233
Visualizações
How to return a array from array of object based on multiple keys

I have an array of object having with multiple value but I have to filter values based on keys into an array and also merge value to array having same value. I have tried in different steps can it be done in single map/filter/reduce?

const a = [
    {abc: ['add', 'edit'], cond: []},
    {cond: ['add_cond', 'edit_cond', 'view_cond'], matx: ['add_matx', 'edit_matx', 'delete_matx'], work: ['add_work', 'edit_work'], def: []},
    {cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx'], work: ['view_work', 'delete_work']},
];
const expected result = {
    cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'],  matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx', 'delete_matx'], work: ['add_work', 'edit_work','view_work', 'delete_work']
}

const b = a.map(el => el.cond);

const c = a.map(el => el.matx);

const d = a.map(el => el.work);

result = {cond: [...b], matx: [...c], work: [...d]}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could collect all wanted properties' values into a set and build a new object from the key/value pairs.

const
    data = [{ abc: ['add', 'edit'], cond: [] }, { cond: ['add_cond', 'edit_cond', 'view_cond'], matx: ['add_matx', 'edit_matx', 'delete_matx'], work: ['add_work', 'edit_work'], def: [] }, { cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx'], work: ['view_work', 'delete_work'] }],
    keys = ['cond', 'matx', 'work'],
    addToSet = (s, v) => s.add(v),
    result = Object.fromEntries(keys.map(k => [
        k, 
        [...data.reduce((r, o) => (o[k] || []).reduce(addToSet, r), new Set)]
    ]));

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

about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve this using a single reduce method. All keys are evaluated dynamically, so no matter how many keys you add/remove to your objects you get dynamically added keys:

const a = [
    {abc: ['add', 'edit'], cond: []},
    {cond: ['add_cond', 'edit_cond', 'view_cond'], matx: ['add_matx', 'edit_matx', 'delete_matx'], work: ['add_work', 'edit_work'], def: []},
    {cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx'], work: ['view_work', 'delete_work']},
];


const result = a.reduce((prev, cur) => {
  // get all keys
  const keys = Object.keys(cur);

  // add each key and related values one by one
  keys.forEach(el => {
    if(prev[el] === undefined)
      prev[el] = []
    prev[el].push(cur[el])
    // remove duplicates
    prev[el] = prev[el].flat()
  })
  return prev
}, {})

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

It can be done with forEach ... .map() and .filter() will always return an array not an object.

const a = [
    {abc: ['add', 'edit'], cond: []},
    {cond: ['add_cond', 'edit_cond', 'view_cond'], matx: ['add_matx', 'edit_matx', 'delete_matx'], work: ['add_work', 'edit_work'], def: []},
    {cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx'], work: ['view_work', 'delete_work']},
];
const result = {cond: [], work: [], matx: []}



a.forEach(x=> { x.cond && result.cond.push(...x.cond); 
                x.work && result.work.push(...x.work);
                 x.matx && result.work.push(...x.matx);
                 }

);

console.log(result);

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