Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
¿Cómo fusionar objetos con las mismas claves?

Tengo una respuesta JSON proveniente de la API de Node.JS donde los valores de etiqueta son los mismos pero otros valores como dd1, dd2 son diferentes. Estoy tratando de fusionar los mismos valores de etiqueta en un objeto . ¿Cómo podría lograr esto?

 { "label": "Medium", "dd1": "Cat A", "dd2": "Non Cision", }, { "label": "Medium", "dd1": "Cat B", "dd2": null, }, { "label": "Tone", "dd1": "Neutral", "dd2": null, }

Arriba de los objetos JSON podemos ver que la etiqueta es la misma en los dos primeros, por lo que debe fusionarse y eliminarse los valores nulos. como esto

 { "label": "Medium", "dd1": "Cat A", "Cat B" "dd2": "Non Cision", }, { "label": "Tone", "dd1": "Neutral", "dd2": null, }

Código TS -

 var output = []; value.forEach(function(item) { var existing = output.filter(function(v, i) { return v.label == item.label; }); if (!existing.length) { if (typeof item.label == "string") output.push(item); } }); console.log(output);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

No sé mucho sobre Typescript; esta es solo una respuesta de Javascript, pero espero que pueda ser útil.

 const data = [ { "label": "Medium", "dd1": "Cat A", "dd2": "Non Cision" }, { "label": "Medium", "dd1": "Cat B", "dd2": null }, { "label": "Medium", "dd1": "Cat C", "dd2": null }, { "label": "Tone", "dd1": "Neutral", "dd2": null }, ]; const merge_value = (key, current_value, new_value) => (key === 'label' || !current_value) ? new_value : !new_value ? current_value : [].concat(current_value, new_value); const merge_objects = (object1, object2) => Object.entries(object2).reduce( (acc, [key, value]) => Object.assign(acc, { [key]: merge_value(key, object1[key], value) }), {} ); const mapped = data.reduce( (acc, item) => acc.set(item.label, merge_objects(acc.get(item.label) ?? {}, item)), new Map() ); const res = [...mapped.values()]; console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

¿Puedes comprobar esto?

 const cars = [ { "label": "Medium", "dd1": "Cat A", "dd2": "Non Cision", }, { "label": "Medium", "dd1": "Cat B", "dd2": null, }, { "label": "Tone", "dd1": "Neutral", "dd2": null, } ]; function groupBy(objectArray, property) { return objectArray.reduce(function (acc, obj) { let key = obj[property] if (!acc[key]) { acc[key] = [] } acc[key].push(obj) return acc }, {}) } function sum(a, c) { if (a && c) { return a + ', ' + c } else if (a) { return a } else if (c) { return c } else { return null } } let object = groupBy(cars, 'label') let arr = []; for (const property in object) { arr.push({ property, dd1: object[property].map(o => o.dd1).reduce((a, c) => { return sum(a, c) }), dd2: object[property].map(o => o.dd2).reduce((a, c) => { return sum(a, c) }) }) } console.log(arr);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!