gpt4 book ai didi

javascript - 转换嵌套的对象数组

转载 作者:行者123 更新时间:2023-11-30 14:44:16 25 4
gpt4 key购买 nike

给定输入数组。

  let data = [
{
name: 'Name',
countries: [
{
name: 'Country1',
competitions: [
{
name: 'Competition1',
matches: [
{ details: 'details' },
{ details: 'details' },
{ details: 'details' }
]
},
{
name: 'Competition2',
matches: [{ details: 'details' }]
}
]
},
{
name: 'Country2',
competitions: [
{
name: 'Competition1',
matches: [{ details: 'details' }]
},
{
name: 'Competition2',
matches: [{ details: 'details' }]
},

{
name: 'Competition3',
matches: [{ details: 'details' }, { detals: 'detail' }]
}
]
}
]
},
{
name: 'Name2',
countries: [
{
name: 'Country1',
competitions: [
{
name: 'Competition1',
matches: [{ details: 'details' }, { details: 'details' }]
}
]
}
]
}
];

和期望的输出:

let sorted = [
// For each Object in orginal array extract all matches and put it in new array.
{ name: 'Name', matches: [{}, {}, {}, {}, {}, {}, {}, {}] },
{ name: 'Name2', matches: [{}, {}] }
]

如何将 data 转换为 sorted 我可以使用 nested 3 nested for 循环并将所有匹配项提取到单个数组中。但它没有给我想要的输出,我觉得嵌套 for 循环对资源不利,我确信它可以更简洁地完成,毕竟我没有得到预期的输出。我试过 Object.assign 在开始时创建新对象等。

像这样使用 for 循环获取 arr 来存储整个 data 数组中的所有匹配项。

  for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].countries.length; j++) {
for (var l = 0; l < data[i].countries[j].competitions.length; l++) {
for (
var k = 0;
k < data[i].countries[j].competitions[l].matches.length;
k++
) {
arr.push(data[i].countries[j].competitions[l].matches[k]);
}
}
}

很碍眼,而且仍然给我留下了所有不好的匹配项的平面数组。

使用 ES6 和新的好东西,我设法更接近解决方案,但留下了我似乎无法展平的嵌套数组。

  const d = data.map(i => {
const inter = i.countries.map(e => e.competitions.map(z => z.matches));

const final = inter.reduce((acc, next) => [...acc, ...next], []);

return {
name: i.name,
matches: final
};
})

出于教育目的,无论此时的性能如何,所有解决方案都将被接受。

tl;dr 如何将 data 转换为 sorted

最佳答案

const sorted = data.map(o => {
const name = o.name;
let matches = [];
o.countries.map(c => {
return c.competitions.forEach(c => {
c.matches.forEach(m => {
matches.push(m);
});
});
});
return { name, matches };
});

我没有使用 map,而是创建了 matches 数组,在遍历来自对象的每个 matches 数组时,我只是将结果放入数组。这是一个片段:

let data = [
{
name: 'Name',
countries: [
{
name: 'Country1',
competitions: [
{
name: 'Competition1',
matches: [
{ details: 'details' },
{ details: 'details' },
{ details: 'details' }
]
},
{
name: 'Competition2',
matches: [{ details: 'details' }]
}
]
},
{
name: 'Country2',
competitions: [
{
name: 'Competition1',
matches: [{ details: 'details' }]
},
{
name: 'Competition2',
matches: [{ details: 'details' }]
},

{
name: 'Competition3',
matches: [{ details: 'details' }, { detals: 'detail' }]
}
]
}
]
}
];
const sorted = data.map(o => {
const name = o.name;
let matches = [];
o.countries.map(c => {
return c.competitions.forEach(c => {
c.matches.forEach(m => {
matches.push(m);
});
});
});
return { name, matches };
});
console.log(sorted);

关于javascript - 转换嵌套的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49191311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com