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

107
Views
¿Cómo puedo hacer una matriz instalada de matrices anidadas en Javascript desde myArray?

Necesito dividir riskTypeNo de cada objeto y luego asignar el valor seleccionado a cada valor y luego crear una matriz con todos los objetos. Con mi código obtengo matrices anidadas

 const myArray = [ { "fieldID": 1681977, "riskTypeNo": "A1;M1", "selectedValue": "4" }, { "fieldID": 1681984, "riskTypeNo": "C6;M1", "selectedValue": "1" }, { "fieldID": 1682084, "riskTypeNo": "A13;C6;M1;D5", "selectedValue": "3" } ]; console.log( myArray.map(item1 => { const riskTypeNo = item1.riskTypeNo.split(";").map(item2 => ({ "riskTypeNo": item2, "selectedValue": item1.selectedValue })); return riskTypeNo; }) );

El resultado deseado es:

 [ { riskTypeNo: "A1", selectedValue: "4" }, { riskTypeNo: "M1", selectedValue: "4" }], [{ riskTypeNo: "C6", selectedValue: "1" }, { riskTypeNo: "M1", selectedValue: "1" }], [{ riskTypeNo: "A13", selectedValue: "3" }, { riskTypeNo: "C6", selectedValue: "3" }, { riskTypeNo: "M1", selectedValue: "3" }, { riskTypeNo: "D5", selectedValue: "3" } }]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede devolver la matriz dividida desde dentro del método de map . Pero eso te dará una serie de matrices. Para aplanar esas matrices, simplemente use el método de matriz flat .

Esto debería funcionar:

 const result = myArray.map(el => { const riskTypes = el.riskTypeNo.split(";").map(riskTypeNo => ({ riskTypeNo, selectedValue: el.selectedValue, })) return riskTypes; }).flat();
about 4 years ago · Juan Pablo Isaza Report

0

Aquí hay una manera:

 const myArray = [ { "fieldID": 1681977, "riskTypeNo": "A1;M1", "selectedValue": "4" }, { "fieldID": 1681984, "riskTypeNo": "C6;M1", "selectedValue": "1" }, { "fieldID": 1682084, "riskTypeNo": "A13;C6;M1;D5", "selectedValue": "3" } ]; const result = myArray.reduce((prev,cur) => { let obj = []; cur.riskTypeNo.split(';').forEach((n) => { obj.push({riskTypeNo: n, selectedValue: cur.selectedValue}); }); prev.push(...obj); return prev; }, []); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

puedes usar este método en su lugar:

 const newArray = []; myArray.forEach((item1) => { item1.riskTypeNo.split(";").forEach((item2) => { newArray.push({ riskTypeNo: item2, selectedValue: item1.selectedValue }); }); });

el resultado estará en newArray .

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!