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

108
Views
How can I make an array instaed of nested arrays in Javascript from myArray?

I need to split riskTypeNo from each object and then assign selectedValue to each value and then create an array with all the objects. With my code I am getting nested arrays

    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;   
  })
);

Desired Result is:

[ 
{ 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

You can return the split array from inside the map method. But that will get you an array of arrays. To flatten those arrays just use the array method flat.

This should work:

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

Here's one way:

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

you can use this method instead :

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

result will be in 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!