组合数组内部的所有数组
Combine All Array Inside of Arrays
我在组合数组时遇到问题,我想从另一个数组中获取这些数组。
数据
datas = [
{
"id": "22333",
"name": "",
"details": [
{
"value": 1
},
{
"value": 2
}
]
},
{
"id": "4444",
"name": "",
"details": [
{
"value": 99
},
{
"value": 66
}
]
}
]
预期输出
final = [
{
"value": 1
},
{
"value": 2
},
{
"value": 99
},
{
"value": 66
}
]
代码
datas.map((data) => ({ ...data.details}))
The flatMap
method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map
followed by a flat
of depth 1, but slightly more efficient than calling those two methods separately.
const datas=[{id:"22333",name:"",details:[{value:1},{value:2}]},{id:"4444",name:"",details:[{value:99},{value:66}]}];
const flattened = datas.flatMap(obj => obj.details);
console.log(flattened);
获取 details
数组中的实际数字 map
。
const datas=[{id:"22333",name:"",details:[{value:1},{value:2}]},{id:"4444",name:"",details:[{value:99},{value:66}]}];
const flattened = datas.flatMap(obj => {
return obj.details.map(el => el.value);
});
console.log(flattened);
datas.reduce((acc, val) => {
return acc.concat(val.details)
}, [])
您也可以在此处使用 reduce
:
arr.reduce((acc, curr) => {
acc.push(...curr.details);
return acc;
}, [])
const arr = (datas = [
{
id: "22333",
name: "",
details: [
{
value: 1,
},
{
value: 2,
},
],
},
{
id: "4444",
name: "",
details: [
{
value: 99,
},
{
value: 66,
},
],
},
]);
const result = arr.reduce((acc, curr) => {
acc.push(...curr.details);
return acc;
}, []);
console.log(result);
我在组合数组时遇到问题,我想从另一个数组中获取这些数组。
数据
datas = [
{
"id": "22333",
"name": "",
"details": [
{
"value": 1
},
{
"value": 2
}
]
},
{
"id": "4444",
"name": "",
"details": [
{
"value": 99
},
{
"value": 66
}
]
}
]
预期输出
final = [
{
"value": 1
},
{
"value": 2
},
{
"value": 99
},
{
"value": 66
}
]
代码
datas.map((data) => ({ ...data.details}))
The
flatMap
method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to amap
followed by aflat
of depth 1, but slightly more efficient than calling those two methods separately.
const datas=[{id:"22333",name:"",details:[{value:1},{value:2}]},{id:"4444",name:"",details:[{value:99},{value:66}]}];
const flattened = datas.flatMap(obj => obj.details);
console.log(flattened);
获取 details
数组中的实际数字 map
。
const datas=[{id:"22333",name:"",details:[{value:1},{value:2}]},{id:"4444",name:"",details:[{value:99},{value:66}]}];
const flattened = datas.flatMap(obj => {
return obj.details.map(el => el.value);
});
console.log(flattened);
datas.reduce((acc, val) => {
return acc.concat(val.details)
}, [])
您也可以在此处使用 reduce
:
arr.reduce((acc, curr) => {
acc.push(...curr.details);
return acc;
}, [])
const arr = (datas = [
{
id: "22333",
name: "",
details: [
{
value: 1,
},
{
value: 2,
},
],
},
{
id: "4444",
name: "",
details: [
{
value: 99,
},
{
value: 66,
},
],
},
]);
const result = arr.reduce((acc, curr) => {
acc.push(...curr.details);
return acc;
}, []);
console.log(result);