gpt4 book ai didi

javascript - 如何使用 javascript Lodash 将所有对象插入另一个数组

转载 作者:行者123 更新时间:2023-11-28 17:28:14 28 4
gpt4 key购买 nike

我进行 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

关于javascript - 如何使用 javascript Lodash 将所有对象插入另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51057697/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com