JS map生成双方括号
JS map generates double square brackets
我使用以下方法合并子数组:
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);
有效;但是,我得到双方括号:
"mySeries3": **[** [
{ "name": "Orange", "data": [0]},
{ "name": "Banana","data": [1]},
{"name": "None", "data": [0]
] **]**
推送的方式正确吗?
我用了@pilchard`
myArray[i].mySeries3 = myArray[i].mySeries3.concat(res)
成功了。`
我使用以下方法合并子数组:
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);
有效;但是,我得到双方括号:
"mySeries3": **[** [
{ "name": "Orange", "data": [0]},
{ "name": "Banana","data": [1]},
{"name": "None", "data": [0]
] **]**
推送的方式正确吗?
我用了@pilchard`
myArray[i].mySeries3 = myArray[i].mySeries3.concat(res)
成功了。`