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

151
Visualizações
Get unique values from multiple array of objects in JS - the right way in javascript

I have these two array of objects and I want to get unique values from them! is anyone know how can I do that in JS?

let array1 = [
    { categoryName: "category 1" },
    { categoryName: "category 2" },
    { categoryName: "category 3" },
    { categoryName: "category 4" },
];

let array5 = [
    { categoryName: "category 1" },
    { categoryName: "category 2" },
    { categoryName: "category 3" },
    { categoryName: "category 4" },
    { categoryName: "category 5" },
];

let newArray = [];

for (const arr5Item of array5) {
  for (const arr1Item of array1) {
    if (arr5Item.categoryName !== arr1Item.categoryName) {
      newArray.push(arr5Item);
      break;
    } else {
      newArray.push(arr1Item);
      break;
    }
  }
}

console.log("newArray ", newArray);

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

0

Array.reduce can solve your problem easily.

Step 1: Merge all of your array into one array, by three dots in javascript.

Step 2: Use Array.reduce to return your result.

let array1 = [
    { categoryName: "category 1" },
    { categoryName: "category 2" },
    { categoryName: "category 3" },
    { categoryName: "category 4" },
];

let array5 = [
    { categoryName: "category 1" },
    { categoryName: "category 2" },
    { categoryName: "category 3" },
    { categoryName: "category 4" },
    { categoryName: "category 5" },
];

const mergedArray = [...array1, ...array5];

const uniqueArray = mergedArray.reduce((acc, item) => {
   if (!acc.some(e => e.categoryName == item.categoryName)) {
      acc.push(item);
   }
   return acc;
}, []);
console.log(uniqueArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can easily achieve the result using Set. Using Set is efficient way

1)

let array1 = [
  { categoryName: "category 1" },
  { categoryName: "category 2" },
  { categoryName: "category 3" },
  { categoryName: "category 4" },
];

let array5 = [
  { categoryName: "category 1" },
  { categoryName: "category 2" },
  { categoryName: "category 3" },
  { categoryName: "category 4" },
  { categoryName: "category 5" },
];

const set = new Set([
  ...array1.map((o) => o.categoryName),
  ...array5.map((o) => o.categoryName),
]);

const result = [...set.keys()].map((s) => ({ categoryName: s }));
console.log(result);

2)

let array1 = [
  { categoryName: "category 1" },
  { categoryName: "category 2" },
  { categoryName: "category 3" },
  { categoryName: "category 4" },
];

let array5 = [
  { categoryName: "category 1" },
  { categoryName: "category 2" },
  { categoryName: "category 3" },
  { categoryName: "category 4" },
  { categoryName: "category 5" },
];

const set = new Set([
  ...array1.map((o) => o.categoryName),
  ...array5.map((o) => o.categoryName),
]);

const result = [];

for (let categoryName of set) result.push({ categoryName });
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Not just one. There are a lot of methods available for this. Basically you need a looping logic.

Please find the Array.reduce implementation.

Logic

  • Create a merged array, by using spread operator.
  • Check each node of merged array in accumulator.
  • If the node from the merged array, doesnot exist in accumulator, then its unique.

Working Fiddle

let array1 = [{ categoryName: "category 1" },{ categoryName: "category 2" },{ categoryName: "category 3" },{ categoryName: "category 4" }];
let array5 = [{ categoryName: "category 1" },{ categoryName: "category 2" },{ categoryName: "category 3" },{ categoryName: "category 4" },{ categoryName: "category 5" }];
const uniqueValues = [...array1, ...array5].reduce((acc, curr) => {
  const nodeExist = acc.some(item => item.categoryName === curr.categoryName);
  const secondArrayExist = array5.some(item => item.categoryName === curr.categoryName);
  if (!nodeExist) {
    acc.push(curr);
  }
  return acc;
}, []);
console.log(uniqueValues);

If you want to get the nodes which are present in only in any one of the array, use the below logic.

Logic

  • Create a merged array.
  • Check each node in the merged array, whether its present in array1 and array5. If the node is not present in any one of array1 or array5 then this is unique node.
  • Push that node to accumulator.

Working Fiddle

let array1 = [{ categoryName: "category 1" },{ categoryName: "category 2" },{ categoryName: "category 3" },{ categoryName: "category 4" }];
let array5 = [{ categoryName: "category 1" },{ categoryName: "category 2" },{ categoryName: "category 3" },{ categoryName: "category 4" },{ categoryName: "category 5" }];
const uniqueValues = [...array1, ...array5].reduce((acc, curr) => {
  const firstArrayExist = array1.some(item => item.categoryName === curr.categoryName);
  const secondArrayExist = array5.some(item => item.categoryName === curr.categoryName);
  if (!firstArrayExist || !secondArrayExist) {
    acc.push(curr);
  }
  return acc;
}, []);
console.log(uniqueValues);

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