我正在尝试展平一个数组,但在其中遇到了一些问题。我有这些数据可以被扁平化。
arr = [
{
"data": [
[
{
"_id": "5ee97ee7f25d1c1482717bdf",
"email": "test1@test.io",
"profileImages": [],
"username": "test1",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location",
"firstName": "test1",
"lastName": "test1",
}
],
[
{
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2@test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]
]
}
],这就是我的.我希望这些数据能够将数据合并到单个数组中,如下所示
data: [
{
"_id": "5ee97ee7f25d1c1482717bdf",
"email": "test1@test.io",
"profileImages": [],
"username": "test1",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location",
"firstName": "test1",
"lastName": "test1"},
{
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2@test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]我曾尝试使用lodash库将其展平,但它不起作用。对于如何将这些数组展平为单个数组,有什么建议吗?
发布于 2020-06-17 13:45:41
您可以通过组合使用flatMap和flat()来做到这一点,flat将展平内部数组,而flatMap将展平外部数组。
var arr = [ { "data": [ [ { "_id": "5ee97ee7f25d1c1482717bdf", "email": "test1@test.io", "profileImages": [], "username": "test1", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location", "firstName": "test1", "lastName": "test1", } ], [ { "_id": "5ee97ef2f25d1c1482717be1", "email": "test2@test.io", "profileImages": [], "username": "test2", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location" } ] ] }];
var result = arr.flatMap(obj=>obj.data.flat());
console.log(result);
我希望这能帮到你。
发布于 2020-06-17 14:01:13
并非所有浏览器都广泛支持flatMap和flat()。看看这个:Search in multidimensional array (Algorithm)
所以简单的递归是最安全的选择。
var data = [
[
{
"_id": "5ee97ee7f25d1c1482717bdf",
"email": "test1@test.io",
"profileImages": [],
"username": "test1",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location",
"firstName": "test1",
"lastName": "test1",
}
],
[
{
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2@test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]
]
var result = [];
function flatten(data){
data.forEach(k=>{
if(Array.isArray(k)){
flatten(k)
}else{
result.push(k)
}
});
return result;
}
console.log(flatten(data))
发布于 2020-06-17 13:49:40
您可以使用flat
var arr = [ { "data": [ [ { "_id": "5ee97ee7f25d1c1482717bdf", "email": "test1@test.io", "profileImages": [], "username": "test1", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location", "firstName": "test1", "lastName": "test1", } ], [ { "_id": "5ee97ef2f25d1c1482717be1", "email": "test2@test.io", "profileImages": [], "username": "test2", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location" } ] ] }];
console.log([...arr[0].data.flat()])
https://stackoverflow.com/questions/62422188
复制相似问题