0

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" } 
}]
2
  • 2
    You are already getting your desired result (which is an array of arrays). Or do you have a typo in "Desired Result is […]" ? Commented May 12, 2022 at 17:27
  • With my code I am getting nested arrays instead of 1 array with objects. Commented May 12, 2022 at 17:28

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

I like this better than mine. +1.
1

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.

Comments

1

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

2 Comments

I don't want arrays within arrays.
Ok. The OP looks like arrays. Updated for one array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.