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

211
Views
cómo fusionar 2 matrices de objetos basadas en claves en JavaScript y conservar los valores de ambas matrices cuando coinciden

Estoy tratando de fusionar 2 matrices de objetos (también pueden ser más) del mismo orden y campos basados en 2 claves. y si las claves coinciden, mantenga los valores de array1 y array2. Por ejemplo:

 var array1 = [ { "id": 11, "smid":21, "name":'test1a', "value":0.5 }, { "id": 12, "smid":22, "name":'test1b', "value":0.6 }, { "id": 13, "smid":null, "name":'test1b', "value":0.7 } ] var array2 = [ { "id": 11, "smid":21, "name":'test2a', "value":1.5 }, { "id": 22, "smid":32, "name":'test2b', "value":1.6 } { "id": 13, "smid":null, "name":'test2c', "value":1.7 }, ]

El resultado tiene que ser

 var resultedArray = [ { "id": 11, "smid":21, "name":'test1a', "value":0.5, "newValue":[0.5,1.5] }, { "id": 12, "smid":22, "name":'test1b', "value":0.6, "newValue":[0.6, null] }, { "id": 13, "smid":null, "name":'test1b', "value":0.7, "newValue":[0.7,1.7] } ]

Puedo lograr el resultado anterior con el siguiente código.

 var idFound = false; for(var ar1Inx = 0; ar1Inx < array1.length; ar1Inx++){ idFound = false; for(var ar2Inx = 0; ar2Inx < array2.length; ar2Inx++){ if(array1[ar1Inx].id == array2[ar2Inx].id && array1[ar1Inx].smid == array2[ar2Inx].smid){ idFound = true; var newArray = []; newArray.push(array1[ar1Inx].value); newArray.push(array2[ar2Inx].value); array1[ar1Inx].["newValue"] = newArray; } } if(!idFound){ var newArray = []; newArray.push(array1[ar1Inx].value); newArray.push(null); array1[ar1Inx].["newValue"] = newArray; } }

Pero, quiero saber, ¿hay alguna manera de lograr el resultado esperado de una manera simple en lugar de iterar a través de cada una de las matrices que es compatible con Internet Explorer 11 con 2 o más matrices? Por favor aconséjame. Gracias por adelantado.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede tomar un objeto como referencia y una matriz para coleccionar.

 // function expects arrays as arguments function leftJoin() { function getKey(object) { return ['id', 'smid'] .map(function (k) { return object[k]; }) .join('|'); } function add(array, index) { for (var i = 0; i < array.length; i++) { var object = array[i], key = getKey(object); if (!reference[key]) continue; reference[key].value[index] = object.value; } } var reference = {}, result = []; for (var i = 0; i < arguments[0].length; i++) { var object = arguments[0][i], key = getKey(object); reference[key] = { id: object.id, smid: object.smid, name: object.name, value: [object.value] }; for (var j = 1; j < arguments.length; j++) reference[key].value[j] = null; result.push(reference[key]); } for (var i = 1; i < arguments.length; i++) add(arguments[i], i); return result; } var array1 = [{ id: 11, smid: 21, name: 'test1a', value: 0.5 }, { id: 12, smid: 22, name: 'test1b', value: 0.6 }, { id: 13, smid: null, name: 'test1b', value: 0.7 }], array2 = [{ id: 11, smid: 21, name: 'test2a', value: 1.5 }, { id: 22, smid: 32, name: 'test2b', value: 1.6 }, { id: 13, smid: null, name: 'test2c', value: 1.7 }]; console.log(leftJoin(array1, array2));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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!