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

280
Vistas
Merge arrays matching a particular key value in JavaScript

I have an array which is like this:

var arr = [{
  "date": "JAN",
  "value": 5,
  "weight": 3
}, {
  "date": "JAN",
  "value": 4,
  "weight": 23
}, {
  "date": "FEB",
  "value": 9,
  "weight": 1
}, {
  "date": "FEB",
  "value": 10,
  "weight": 30
}]

I want to match the primary key which is heredate. Matching this I want to merge the rest of the key values and get this following output:

[{
  "date": "JAN",
  "value": [5, 4],
  "weight": [3, 23]
}, {
  "date": "FEB",
  "value": [9, 10],
  "weight": [1, 30]
}]

I have written a function like this but can't figure out how to concat the key values:

var arr = [{
  "date": "JAN",
  "value": 5,
  "weight": 3
}, {
  "date": "JAN",
  "value": 4,
  "weight": 23
}, {
  "date": "FEB",
  "value": 9,
  "weight": 1
}, {
  "date": "FEB",
  "value": 10,
  "weight": 30
}]

const transform = (arr, primaryKey) => {
  var newValue = [];
  for (let i = 0; i < arr.length; i++) {
    for (let j = 1; j < arr.length; j++) {
      if (primaryKey[i] === primaryKey[j]) {
        newValue.push({
          ...arr[i],
          ...arr[j]
        });
      }
    }
  }
  return newValue
};

console.log(transform(arr,'date'))

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

  • Using Array#reduce, iterate over the list while updating a Map where the key is the primary-key and the value is the grouped object. In every iteration, create/update the pair.
  • Using Map#values, return the list of grouped objects

const transform = (arr, primaryKey) => [...
  arr.reduce((map, { [primaryKey]: key, ...e }) => {
    const { [primaryKey]: k, ...props } = map.get(key) ?? {};
    for(let prop in e) {
      props[prop] = [...(props[prop] ?? []), e[prop]];
    }
    map.set(key, { [primaryKey]: key, ...props });
    return map;
  }, new Map)
  .values()
];

const arr = [ { "date": "JAN", "value": 5, "weight": 3 }, { "date": "JAN", "value": 4, "weight": 23 }, { "date": "FEB", "value": 9, "weight": 1 }, { "date": "FEB", "value": 10, "weight": 30 } ]; 
console.log( transform(arr, 'date') );

over 4 years ago · Santiago Trujillo Denunciar

0

The following code should work:

const transform = (arr, primaryKey) => {
    var newValue = [];
    for(let i = 0; i < arr.length; i++){
        arr[i]["value"] = [arr[i]["value"]];
        arr[i]["weight"] = [arr[i]["weight"]];
    }
    newValue.push(arr[0])
    for(let i = 1; i < arr.length; i++){
        let contains = false;
        for(let j = 0; j < newValue.length; j++){
            if(newValue[j][primaryKey] == arr[i][primaryKey]){
                newValue[j]["value"].push(arr[i]["value"][0]);
                newValue[j]["weight"].push(arr[i]["weight"][0]);
                contains = true;
            }
        }
        if(!contains){
            newValue.push(arr[i]);
        }
        
    }
   return newValue
};

var arr = [{
              "date": "JAN",
              "value": 5,
              "weight": 3
          }, {
              "date": "JAN",
              "value": 4,
              "weight": 23
          }, {
              "date": "FEB",
              "value": 9,
              "weight": 1
          }, {
              "date": "FEB",
              "value": 10,
              "weight": 30
          }] 
          
var newthing = transform(arr,"date");
console.log(newthing);

Output:

[ { date: 'JAN', value: [ 5, 4 ], weight: [ 3, 23 ] },
  { date: 'FEB', value: [ 9, 10 ], weight: [ 1, 30 ] } ]

The way this code works is that first, we turn the values of the keys for "value" and "weight" into lists.

Then, we begin by pushing the first element of arr into newValue.

From here, we do a nested for loop to iterate through the remaining of arr and newValue:

If the value of "date" for every element of arr already exists in newValue, then we will push in the values of "value" and "weight" that belongs to arr.

However, if it does not exist, then we will simply push that element inside of newValue.

I hope this helped answer your question! Pleas let me know if you need any further help or clarification :)

over 4 years ago · Santiago Trujillo Denunciar

0

Combining a couple of reduce can also do the same job:

const arr = [{
              "date": "JAN",
              "value": 5,
              "weight": 3
          }, {
              "date": "JAN",
              "value": 4,
              "weight": 23
          }, {
              "date": "FEB",
              "value": 9,
              "weight": 1
          }, {
              "date": "FEB",
              "value": 10,
              "weight": 30
          }] 

const arrayMappedByDate = arr.reduce((acc, curData) => {
  if (acc[curData.date]) {
    acc[curData.date].push(curData)
  } else {
    acc[curData.date] = [curData]
  }
  return acc
}, {})

const transformedArray = Object.entries(arrayMappedByDate).map(([dateInit, data]) => {
 const normalized = data.reduce((acc, cur) => {
   if (acc.date) {
     acc.value.push(cur.value)
     acc.weight.push(cur.weight)
   } else {
     acc = { 
       date: cur.date,
       value: [cur.value],
       weight: [cur.weight]
     }
   }
   return acc
 }, {})
 return { [dateInit]: normalized }
})


 console.log(transformedArray)
over 4 years ago · Santiago Trujillo 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