Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
How to combine tow array and remove duplicate object from array

I have two arrays like this and I have filtered these two arrays and combined them in a final array and remove the duplicate value and keep the one

let arr1 = [
  {
    name : 'abc',
    std: '1'
  },
  {
    name : 'abc1',
    std: '2'
  },
  {
    name : 'abc2',
    std: '3'
  },
]
let arr2 = [
  {
    name : 'abc3',
    std: '1'
  },
  {
    name : 'abc4',
    std: '2'
  },
  {
    name : 'abc2',
    std: '3'
  },
]

expected output like I want abc2 get only one time

let finalArr = [
  {
    name : 'abc',
    std: '1'
  },
  {
    name : 'abc1',
    std: '2'
  },
  {
    name : 'abc2',
    std: '3'
  },
  {
    name : 'abc3',
    std: '1'
  },
  {
    name : 'abc4',
    std: '2'
  },
]

so can anyone help me out with how can I do that I tried many ways but not working and som time I got error.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could use a Set for the given name and filter merged array with the set.

let arr1 = [{name : 'abc',std: '1'},{name : 'abc1',std: '2'},{ name : 'abc2', std: '3'}]

let arr2 = [{name : 'abc3', std: '1' }, { name : 'abc4', std: '2' }, {name : 'abc2', std: '3'}]

const names = new Set(arr1.map(({name}) => name));
const filteredArray = [...arr1, ...arr2.filter(({name}) => !names.has(name))];


console.log(filteredArray)
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Ids should be unique in the array

arr.reduce((acc, current) => {
  const x = acc.find(item => item.id === current.id);
  if (!x) {
    return acc.concat([current]);
  } else {
    return acc;
  }
}, []);
about 4 years ago · Juan Pablo Isaza Denunciar

0

There's an other way too, you have options :)

const arr1 = [{
  name: 'abc',
  std: '1'
}, {
  name: 'abc1',
  std: '2'
}, {
  name: 'abc2',
  std: '3'
}, ]
const arr2 = [{
  name: 'abc3',
  std: '1'
}, {
  name: 'abc4',
  std: '2'
}, {
  name: 'abc2',
  std: '3'
}, ]

function mapper(v) {
  return [v.name, v]
};
const map = new Map(arr1.map(mapper).concat(arr2.map(mapper)));
console.log([...map.values()])

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda