给定输入数组。
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
转换为Object.assign
我可以使用嵌套的3嵌套for循环并将所有匹配提取到单个数组中。但是它没有给我想要的输出,我觉得嵌套for循环对资源是坏的,我确信它可以更简洁地完成,毕竟我没有得到预期的输出。我尝试arr
在启动时创建新对象等。
使用for循环让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]);
}
}
}
数组的所有匹配项。
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
};
})
是眼睛,仍然留下我所有比赛的平面阵列,这是不好的。
使用ES6和新的好东西我设法接近解决方案,但留下了我似乎无法展平的嵌套数组阵列。
data
出于教育目的,无论此时的表现如何,所有解决方案都将被接受。
tl; dr如何将sorted
转换为 $pdf = new Mpdf(['mode' => 'utf-8']);
$pdf->WriteHTML(view()->make('reports.transactions.report', [
'transactions' => $transactions,
'from' => $start ? $start->format('d-M-Y') : '',
'to' => $end ? $end->format('d-M-Y') : '',
'generatedOn' => Carbon::now()->format('d-M-Y h:i:s'),
'credit' => $this->getTotalCredits($transactions),
'debit' => $this->getTotalDebits($transactions),
]));
return $pdf->Output('transaction_pdf.pdf','D');
答案 0 :(得分:1)
您可以使用reduce
和concat
匹配
let result = data.map(v=>{
let inter = v.countries.reduce((c,v)=> c.concat( v.competitions.map(z => z.matches).reduce((x,z) => x.concat( z ),[] ) ),[]);
return {
name : v.name,
matches : inter
}
});
这是一个片段:
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'
},
{
details: 'details'
}
]
},
{
name: 'Competition2',
matches: [{
details: 'details'
}]
}
]
}, ]
}
];
let result = data.map(v => {
let inter = v.countries.reduce((c, v) => c.concat(v.competitions.map(z => z.matches).reduce((x, z) => x.concat(z), [])), []);
return {
name: v.name,
matches: inter
}
});
console.log(result);
答案 1 :(得分:1)
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);
&#13;
答案 2 :(得分:0)
在解析过程中可能更容易过滤:
let sorted = [], last = [], json = '[{"name":"Name","countries":[{"name":"Country1","competitions":[{"name":"Competition1","matches":[{"details":"details1"},{"details":"details2"},{"details":"details3"}]},{"name":"Competition2","matches":[{"details":"details4"}]}]},{"name":"Country2","competitions":[{"name":"Competition1","matches":[{"details":"details5"}]},{"name":"Competition2","matches":[{"details":"details6"}]},{"name":"Competition3","matches":[{"details":"details7"},{"detals":"detail8"}]}]}]},{"name":"Name2","countries":[{"name":"Country1","competitions":[{"name":"Competition1","matches":[{"details":"details9"},{"details":"details10"}]}]}]}]'
JSON.parse(json, (k, v) => k === 'details' ? last.push(v) :
v.countries !== void 0 ? (sorted.push({ name: v.name, matches: last }), last = []) : 0)
console.log( sorted )