javascript - 如何使用 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
推荐阅读
- perl - 在 /root/perl5/lib/perl5/Parallel/ForkManager.pm 第 73 行的模式匹配 (m//) 中使用未初始化的值 $args[0]
- jmeter - 无法定位元素:{"method":"xpath","selector":"//button[contains(text(),'New patient')]"}
- python - Pandas: Plot a histogram of times in intervals of 10 minutes
- javascript - img 嵌入式 SVG 在将其转换为内联 SVG 后不会改变颜色
- dojo - Dojo 1.8.1 的支持级别
- entity-framework - 未捕获实体框架查询的扩展事件(读取/更新)
- mysql - 如何使用 GROUP BY 两次但不使用 JOIN 本身?
- ruby-on-rails - NoMethodError:未定义的方法`liquidity_manager?为了 #
- svg - gatsby image svg not found
- django - Angular - 注册用户时出现 Django REST api“错误请求”错误