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

111
Views
Cómo mutar un objeto en una matriz si tiene la misma clave usando javascript

Amablemente ayúdame a mutar el objeto en javascript. Digamos que tengo una lista de objetos a continuación, quiero objetos si las claves son las mismas en la primera lista Clave "ANTEPIÉ - EXTERIOR" repetida dos veces, el valor de esta clave es diferente, en la primera aparición la izquierda es verdadera y en la segunda aparición la derecha es verdadera, quiero que esto se fusione como "forefoot - outer" : {left: true, right: true}

 [ { "FOREFOOT - OUTER": { "left": true, "right": false } }, { "FOREFOOT": { "left": false, "right": true } }, { "FOREFOOT - INNER": { "left": false, "right": true } }, { "FOREFOOT - OUTER": { "left": false, "right": true } }, ]

La respuesta esperada es

 [ { "FOREFOOT": { "left": false, "right": true } }, { "FOREFOOT - INNER": { "left": false, "right": true } }, { "FOREFOOT - OUTER": { "left": true, "right": true } }, ]

¿Alguien puede ayudarme a encontrar una solución para esto en javascript ES6?

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

0

Podrías hacer una pequeña función como esta:

 const filterArray = (arrayToFilter) => { const newArray = arrayToFilter; let i = -1; while (++i < newArray.length - 1) { // So we brows the array since the start finally. j = i; while (++j < newArray.length) { // We create a string to could use it in our if const item_i = Object.keys(newArray[i]) + ''; const item_j = Object.keys(newArray[j]) + ''; // If the 2nd occure is true, then we put the 1st true if (item_i == item_j) { if (newArray[j][item_j].left) newArray[i][item_i].left = true; if (newArray[j][item_j].right) newArray[i][item_i].right = true; newArray.splice(j, 1); // Seeing that we delete an element, we decrease our J j--; } } } return newArray; }

Entonces esto es para fusionar solo el valor "verdadero". Porque en su muestra fusiona solo el valor Verdadero. Si también desea fusiones falsas, edite el if con:

 if (item_i == item_j) { newArray[i][item_i].left = newArray[j][item_j].left; newArray[i][item_i].right = newArray[j][item_j].right; newArray.splice(j, 1); j--; }
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!