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

146
Visualizações
Get unique values from array values in json array

I'm writing a code where I need to return uniques values from a JSON array. Here my challenge is, I've got these values as an array for one of the keys.

Here is my code.

let mobilePhones = [{
  id: 1,
  brand: ["B1", "B2"]
}, {
  id: 2,
  brand: ["B2"]
}, {
  id: 3,
  brand: ["B1", "B2"]
}, {
  id: 4,
  brand: ["B1"]
}, {
  id: 5,
  brand: ["B2", "B1"]
}, {
  id: 6,
  brand: ["B3"]
}]
let allBrandsArr = mobilePhones.map(row => {
  return row.brand;
});
let uniqueBrands = allBrandsArr.filter((item, index, arry) => (arry.indexOf(item) === index));
console.log(JSON.stringify(uniqueBrands));

Here my expected result is to get ["B1", "B2", "B3"]. Please let me know how can I achieve this.

Updated new sample data:

let mobilePhones = [{
      id: 1,
      brand: ["B1, B2"]
    }, {
      id: 2,
      brand: ["B2"]
    }, {
      id: 3,
      brand: ["B1, B2"]
    }, {
      id: 4,
      brand: ["B1"]
    }, {
      id: 5,
      brand: ["B2, B1"]
    }, {
      id: 6,
      brand: ["B3"]
    }]
    let allBrandsArr = mobilePhones.map(row => {
      return row.brand;
    });

Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to use flat for merge sub array then your code was good:

let mobilePhones = [{
  id: 1,
  brand: ["B1, B2"]
}, {
  id: 2,
  brand: ["B2"]
}, {
  id: 3,
  brand: ["B1, B2"]
}, {
  id: 4,
  brand: ["B1"]
}, {
  id: 5,
  brand: ["B2, B1"]
}, {
  id: 6,
  brand: ["B3"]
}]
let allBrandsArr = mobilePhones.map(row => {
  return row.brand[0].split(',').map(function(item) {
    return item.trim();
  });
});
let uniqueBrands = allBrandsArr.flat().filter((item, index, arry) => (arry.indexOf(item) === index));
console.log(JSON.stringify(uniqueBrands));

Reference:

  • Array.prototype.flat()

After new Data posted i add split with trim.

Reference:

  • String.prototype.split()
  • String.prototype.trim()
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use .flatMap to get all the brand values and pass it to a Set to make it unique.

const uniqueBrands = [...new Set(mobilePhones.flatMap(({
  brand
}) => brand))];

let mobilePhones = [{
  id: 1,
  brand: ["B1", "B2"]
}, {
  id: 2,
  brand: ["B2"]
}, {
  id: 3,
  brand: ["B1", "B2"]
}, {
  id: 4,
  brand: ["B1"]
}, {
  id: 5,
  brand: ["B2", "B1"]
}, {
  id: 6,
  brand: ["B3"]
}]

const unique = [...new Set(mobilePhones.flatMap(({
  brand
}) => brand))];

console.log(unique);

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