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" }
}]