使用 map() 或类似函数 return 以下格式的数组数据
Using map() or similar function to return the array data in the following format
我有以下 javascript 数组(我今天玩 map() 很开心)- 我希望能够 return pages
数据但有 page id
作为键,页面数组中该页面位置的索引作为值。我做错了什么?
let result = [
{
"id": 10000089,
"units": [
{
"id": 10000200,
"pages": [
{
"id": 100000882
}
]
},
{
"id": 10000340,
"pages": [
{
"id": 100000912
},
{
"id": 100000915
},
{
"id": 100000919
}
]
}
],
}
];
// this is my attempt but doesn't return in the intended format below
result.flatMap(el => el.units.map((e, i) => (e.pages)));
预期输出
pages = [
100000882 => 0,
100000912 => 0,
100000915 => 1,
100000919 => 2,
]
这是代码的堆栈闪电战
https://stackblitz.com/edit/js-mc9rqe
在你的数据中,pages
也是一个对象数组。因此,您还需要遍历每个页面。
- 使用
Array.flat
let result=[{id:10000089,units:[{id:10000200,pages:[{id:100000882}]},{id:10000340,pages:[{id:100000912},{id:100000915},{id:100000919}]}]}];
const getFormattedData = data => {
const res = data.map(datum => datum.units.map(unit => unit.pages.map(({ id }, i) => ({
[id]: i
}))));
return res.flat(2);
}
console.log(getFormattedData(result));
- 使用
Array.flatMap
let result=[{id:10000089,units:[{id:10000200,pages:[{id:100000882}]},{id:10000340,pages:[{id:100000912},{id:100000915},{id:100000919}]}]}];
const getFormattedData = data => {
return data.flatMap(datum => datum.units.flatMap(unit => unit.pages.map(({ id }, i) => ({
[id]: i
}))));
}
console.log(getFormattedData(result));
请注意,上述两种方法都会生成对象数组。
您的预期输出应该是 object
而不是 array
。您可以使用 Array.prototype.flatMap
, Object.fromEntries
来获得结果。
let result=[{id:10000089,units:[{id:10000200,pages:[{id:100000882}]},{id:10000340,pages:[{id:100000912},{id:100000915},{id:100000919}]}]}];
const pages = Object.fromEntries(
result.flatMap(item => item.units.flatMap(unit => unit.pages.map((page,i) => ([page.id, i]))))
);
console.log(pages);
注意,Object.fromEntries()
取一个[key, value]
对数组的数组,然后将它们转换成一个对象。在您的情况下,page.id
将是 key
,最后一张地图的 index
将是 value
.
我有以下 javascript 数组(我今天玩 map() 很开心)- 我希望能够 return pages
数据但有 page id
作为键,页面数组中该页面位置的索引作为值。我做错了什么?
let result = [
{
"id": 10000089,
"units": [
{
"id": 10000200,
"pages": [
{
"id": 100000882
}
]
},
{
"id": 10000340,
"pages": [
{
"id": 100000912
},
{
"id": 100000915
},
{
"id": 100000919
}
]
}
],
}
];
// this is my attempt but doesn't return in the intended format below
result.flatMap(el => el.units.map((e, i) => (e.pages)));
预期输出
pages = [
100000882 => 0,
100000912 => 0,
100000915 => 1,
100000919 => 2,
]
这是代码的堆栈闪电战 https://stackblitz.com/edit/js-mc9rqe
在你的数据中,pages
也是一个对象数组。因此,您还需要遍历每个页面。
- 使用
Array.flat
let result=[{id:10000089,units:[{id:10000200,pages:[{id:100000882}]},{id:10000340,pages:[{id:100000912},{id:100000915},{id:100000919}]}]}];
const getFormattedData = data => {
const res = data.map(datum => datum.units.map(unit => unit.pages.map(({ id }, i) => ({
[id]: i
}))));
return res.flat(2);
}
console.log(getFormattedData(result));
- 使用
Array.flatMap
let result=[{id:10000089,units:[{id:10000200,pages:[{id:100000882}]},{id:10000340,pages:[{id:100000912},{id:100000915},{id:100000919}]}]}];
const getFormattedData = data => {
return data.flatMap(datum => datum.units.flatMap(unit => unit.pages.map(({ id }, i) => ({
[id]: i
}))));
}
console.log(getFormattedData(result));
请注意,上述两种方法都会生成对象数组。
您的预期输出应该是 object
而不是 array
。您可以使用 Array.prototype.flatMap
, Object.fromEntries
来获得结果。
let result=[{id:10000089,units:[{id:10000200,pages:[{id:100000882}]},{id:10000340,pages:[{id:100000912},{id:100000915},{id:100000919}]}]}];
const pages = Object.fromEntries(
result.flatMap(item => item.units.flatMap(unit => unit.pages.map((page,i) => ([page.id, i]))))
);
console.log(pages);
注意,Object.fromEntries()
取一个[key, value]
对数组的数组,然后将它们转换成一个对象。在您的情况下,page.id
将是 key
,最后一张地图的 index
将是 value
.