如何展平这种阵列?
How to flatten this kind of arrays?
我正在尝试展平数组,但遇到了一些问题。我有这些数据可以展平。
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 库来展平它,但没有成功。关于如何将这些数组展平为单个数组的任何建议?
您可以通过使用 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);
希望对您有所帮助。
你可以使用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()])
flatMap
和 flat()
并未在所有浏览器中得到广泛支持。看到这个:
所以简单的递归是最安全的选择。
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))
我正在尝试展平数组,但遇到了一些问题。我有这些数据可以展平。
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 库来展平它,但没有成功。关于如何将这些数组展平为单个数组的任何建议?
您可以通过使用 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);
希望对您有所帮助。
你可以使用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()])
flatMap
和 flat()
并未在所有浏览器中得到广泛支持。看到这个:
所以简单的递归是最安全的选择。
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))