[英]How to push all objects into another array using javascript Lodash
我通過Web API調用,並且在后台運行了不止一次。 因此,我收到了響應commentRows作為提及的數據。
我必須將這些數據映射到另一個數組中。
var arrPush =[ ];
var commentRows ={
'@odata.context':'https:indexes',
value:[
{
"key":"176611",
"status":true,
"errorMessage":null,
"statusCode":200
},
{
"key":"176100",
"status":true,
"errorMessage":null,
"statusCode":200
}
]
};
arrPush.push(commentRows.value);
它為as生成數組,
[
[
{
"key":"176611",
"status":true,
"errorMessage":null,
"statusCode":200
},
{
"key":"176100",
"status":true,
"errorMessage":null,
"statusCode":200
}
]
]
需要
[
{
"key":"176611",
"status":true,
"errorMessage":null,
"statusCode":200
},
{
"key":"176100",
"status":true,
"errorMessage":null,
"statusCode":200
}
]
但我不希望在第一個[]后面附加我可以使用Lodash來實現這一點嗎
這是一個可以處理value
數組中多個元素的解決方案:
使用_.flatten :
var arrPush = [];
var commentRows = {
'@odata.context':'https:indexes',
value:[
{
"key":"176611",
"status":true,
"errorMessage":null,
"statusCode":200
}
]
};
arrPush.push(commentRows.value);
arrPush = _.flatten(arrPush); // here you get it
var arrPush = []; var commentRows = { '@odata.context':'https:indexes', value:[ { "key":"176611", "status":true, "errorMessage":null, "statusCode":200 } ] }; commentRows.value.forEach(x => arrPush.push(x)) console.log(arrPush);
使用forEach
循環並將其推入arrPush
像這樣推送arrPush.push(commentRows.value [0]);
您可以使用
arrPush = _.concat(arrPush, commentRows.value[0]).
在這里,arrayToCopy是要將數據復制到的數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.