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

121
Vistas
How to I remove duplicates and append multi layer JSON array?

I have an array which has different location and items.

[
    {"location": "north", "items" : ["IT1", "IT2"]},
    {"location": "south", "items" : ["IT1", "IT2"]},
    {"location": "north", "items" : ["IT3", "IT4"]}
]

I want to remove duplicate location fields and append the items property from the duplicate to achieve this:

[
    {"location": "north", "items" : ["IT1", "IT2", "IT3", "IT4"]},
    {"location": "south", "items" : ["IT1", "IT2"]}
]

How can I do it with vanilla JS?

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

0

By adapting How to group or merge this array of objects in javascript? to accumulate arrays instead of numbers, here's the result:

var arr = [{
    "location": "north",
    "items": ["IT1", "IT2"]
  },
  {
    "location": "south",
    "items": ["IT1", "IT2"]
  },
  {
    "location": "north",
    "items": ["IT3", "IT4"]
  }
];

var finalArr = arr.reduce((m, o) => {
  var found = m.find(p => p.location === o.location);
  if (found) {
    found.items = found.items.concat(o.items);
  } else {
    m.push(o);
  }
  return m;
}, []);

console.log(finalArr)

about 4 years ago · Juan Pablo Isaza Denunciar

0

I hope it would work for you


var data = [
    {"location": "north", "items" : ["IT1", "IT2"]},
    {"location": "south", "items" : ["IT1", "IT2"]},
    {"location": "north", "items" : ["IT3", "IT4"]}
];

const result = data.filter((thing, index, self) =>
  index === self.findIndex((t) => (
    t.location === thing.location
  ))
)
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Build an object with keys as location and values as aggregated items.

const process = (arr, track = {}) => {
  arr.forEach((obj) => {
    if (track[obj.location]) {
      track[obj.location] = {
        ...track[obj.location],
        items: obj.items.concat(track[obj.location].items),
      };
    } else {
      track[obj.location] = { ...obj };
    }
  });
  return Object.values(track);
};

const data = [
  { location: "north", items: ["IT1", "IT2"] },
  { location: "south", items: ["IT1", "IT2"] },
  { location: "north", items: ["IT3", "IT4"] },
];

console.log(process(data));

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