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

140
Visualizações
group array of objects with inner array of strings javascript

I have an array of objects like below -

const books = [
  {
    name:"abc",
    isbn: 123,
    tags: ["tagA","tagB","tagC"]
  },
  {
    name:"xyz",
    isbn: 456,
    tags: ["tagB","tagC"]
  },
  {
    name:"pqr",
    isbn: 456,
    tags: ["tagB"]
  }
];

I want to group it based on tags of each object, and push the matched objects into the tags values which is string array. My expected output shoulb be an object having the grouped value as key and the value should be the array of matched values.

My Expected Output is-

const expected = {
    "tagA" : [
     {
         name:"abc",
       isbn: 123,  
     },
  ],
    "tagB" : [
     {
         name:"abc",
       isbn: 123,  
     },
     {
         name:"xyz",
       isbn: 456,  
     },
     {
         name:"pqr",
       isbn: 456,  
     },
  ],
    "tagC" : [
      {
         name:"abc",
       isbn: 123,  
     },
     {
         name:"xyz",
       isbn: 456,  
     },
  ],
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are similar questions for grouping by key value; since you want to group based on value of key which is array, you could do something like this:

const books = [
  {
    name:"abc",
    isbn: 123,
    tags: ["tagA","tagB","tagC"]
  },
  {
    name:"xyz",
    isbn: 456,
    tags: ["tagB","tagC"]
  },
  {
    name:"pqr",
    isbn: 456,
    tags: ["tagB"]
  }
];

let grouped = {};

books.forEach((book)=>{

 book.tags.forEach(tag=>{
   if(!grouped[tag]) grouped[tag] = [{name:book.name,isbn:book.isbn}]
   else grouped[tag].push({name:book.name,isbn:book.isbn})
 })

})

console.log(grouped)

about 4 years ago · Juan Pablo Isaza Relatório

0

This is a very standard 'group by' with an extra loop to add each element to multiple groups.

Here using destructuring to isolate the tags array from the rest of the book object, iterating over each tag creating a property in the result using logical nullish assignment (??=) if it doesn't already exist, and then pushing a clone of each book using spread syntax

const books = [{ name: "abc", isbn: 123, tags: ["tagA", "tagB", "tagC"] }, { name: "xyz", isbn: 456, tags: ["tagB", "tagC"] }, { name: "pqr", isbn: 456, tags: ["tagB"] }];

const result = {};
for (const { tags, ...book } of books) {
  for (const tag of tags) {
    (result[tag] ??= []).push({...book});
  }
}

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