0

I use the following to merge sub-arrays:

var myArray = [ { "QuestionId": 1, "mySeries": [{ "name": "Male", "data": [1] }], "mySeries1": [ { "name": "Male", "data": [0] }, { "name": "Female", "data": [0] }, { "name": "Unknown", "data": [0] } ], "mySeries3": [] }, { "QuestionId": 2, "mySeries": [{ "name": "Banana", "data": [1] }], "mySeries1": [ { "name": "Orange", "data": [0] }, { "name": "Banana", "data": [0] }, { "name": "None", "data": [0] } ], "mySeries3": [] } ]; var res = []; for (let i = 0; i < myArray.length; i++) { res = myArray[i].mySeries1.map(obj => myArray[i].mySeries.find(o => o.name === obj.name) || obj); myArray[i].mySeries3.push(res); 

It works; however, I get double square brackets:

"mySeries3": **[** [ { "name": "Orange", "data": [0]}, { "name": "Banana","data": [1]}, {"name": "None", "data": [0] ] **]** 

Is push the correct way?

1
  • 2
    You're pushing an array into an array, so of course it will. You can instead assign it to the property. myArray[i].mySeries3 = res or if you need to add to it you can concat() or use spread syntax. myArray[i].mySeries3 = myArray[i].mySeries3.concat(res) Commented Apr 17, 2022 at 0:49

1 Answer 1

0

I used @pilchard `

myArray[i].mySeries3 = myArray[i].mySeries3.concat(res) 

It worked.`

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.