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

144
Vistas
How to map this specific array of objects to the array grouped by the common key value

I have this simplified array of objects

var items = [{"function":"function_1","process":"process_1"}, {"function":"function_1","process":"process_2"}, {"function":"function_1","process":"process_3"},  {"function":"function_2","process":"process_3"},  {"function":"function_2","process":"process_4"}]

that I want to map in JS according to the keys into the following array:

result = [
{
    "function":"function_1",
    "process": [
      "process_1",
      "process_2" 
  ]
}, 
{
    "function":"function_2",
    "process": [
      "process_3",
      "process_4" 
  ]
}
]

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

0

Dedicated to you my friend

var items = [{"function":"function_1","process":"process_1"}, {"function":"function_1","process":"process_2"}, {"function":"function_1","process":"process_3"},  {"function":"function_2","process":"process_3"},  {"function":"function_2","process":"process_4"}]


var arr2 = items.reduce( (a,b) => {
  
    var i = a.findIndex( x => x.function === b.function);
  
    return i === -1 ? a.push({ function : b.function, process : [b.process] }) : a[i].process.push(b.process), a;
  
}, []);


console.log(arr2)

about 4 years ago · Juan Pablo Isaza Denunciar

0

var items = [{
  "function": "function_1",
  "process": "process_1"
}, {
  "function": "function_1",
  "process": "process_2"
}, {
  "function": "function_1",
  "process": "process_3"
}, {
  "function": "function_2",
  "process": "process_3"
}, {
  "function": "function_2",
  "process": "process_4"
}]

var uniqueFunctionList = [] // to keep track of unique function names
var resultArr = []          
items.forEach(item => {
  if (!uniqueFunctionList.includes(item.function)) { // item object doesnt exist
    uniqueFunctionList.push(item.function)           // add unique function name
    let tmp_obj = {}
    tmp_obj['function'] = item.function              // item is unique just push it
    tmp_obj['process'] = [item.process]              // make process array via []
    resultArr.push(tmp_obj)                          
  } else {                                           // function name is not unique
    resultArr.forEach(result => {                    // it is available in resultArr
      if (result.function == item.function) {        // find the function
        result.process.push(item.process)            // push to process array  
      }
    })
  }
})

console.log(resultArr)

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