我有一個物件,其中 Citems 是一個物件陣列。每個物件都具有 on 或 of 和 time 的狀態。
{
Chapter: [
{
Cname: 'chapter 1',
Citems: [{status: 'on', time: 30},{status: 'on', time: 60}],
},
{
Cname: 'chapter 2',
Citems: [{status: 'on', time: 30},{status: 'off', time: 60}]
}
],
name: 'Something',
description: 'jfdgljfgdfjgldfkjglfd'
}
我想從中生成一個陣列或物件,顯示每個狀態的總時間,如下所示
{
on: 120,
off: 60
}
我嘗試使用 map 和 reduce,但感到困惑。
uj5u.com熱心網友回復:
您只需要一個嵌套的“總和”,這里使用reduce()并利用計算屬性status來使用as 鍵更新累加器。
const data = { Chapter: [{ Cname: 'chapter 1', Citems: [{ status: 'on', time: 30 }, { status: 'on', time: 60 }], }, { Cname: 'chapter 2', Citems: [{ status: 'on', time: 30 }, { status: 'off', time: 60 }] }], name: 'Something', description: 'jfdgljfgdfjgldfkjglfd' };
const result = data.Chapter.reduce((a, { Citems }) => {
for (const { status, time } of Citems) {
a[status] = time;
}
return a;
}, { on: 0, off: 0 });
console.log(result);
或使用for...of回圈
const data = { Chapter: [{ Cname: 'chapter 1', Citems: [{ status: 'on', time: 30 }, { status: 'on', time: 60 }], }, { Cname: 'chapter 2', Citems: [{ status: 'on', time: 30 }, { status: 'off', time: 60 }] }], name: 'Something', description: 'jfdgljfgdfjgldfkjglfd' }
const result = { on: 0, off: 0 };
for (const { Citems } of data.Chapter) {
for (const { status, time } of Citems) {
result[status] = time;
}
}
console.log(result);
要將其擴展到此類Chapter物件的陣列,您可以將其再次嵌套在reduce().
顯示代碼片段
const data = [
{
Chapter: [{ Cname: 'chapter 1', Citems: [{ status: 'on', time: 30 }, { status: 'on', time: 60 }], }, { Cname: 'chapter 2', Citems: [{ status: 'on', time: 30 }, { status: 'off', time: 60 }] }],
name: 'Something',
description: 'jfdgljfgdfjgldfkjglfd'
},
{
Chapter: [{ Cname: 'chapter 1', Citems: [{ status: 'on', time: 30 }, { status: 'off', time: 30 }], }, { Cname: 'chapter 2', Citems: [{ status: 'on', time: 30 }, { status: 'off', time: 60 }] }],
name: 'Something2',
description: 'asdfasdfasdfasdfasdfa'
}
]
const result = data.reduce((a, { name, Chapter }) => {
a[name] = Chapter.reduce((a, { Citems }) => {
for (const { status, time } of Citems) {
a[status] = time;
}
return a;
}, { on: 0, off: 0 });
return a;
}, {});
console.log(result);
uj5u.com熱心網友回復:
let obj = {Chapter: [{_id: '624568b157da1d351910c576',Cname:'chapter 1',Citems: [{status: 'on',time: 30},{status: 'on',time: 60}],},{_id:'456a5857da1d351910c577',Cname: 'chapter 2',Citems: [{status:'on',time: 30},{status: 'off',time: 60}]}],_id: '6245f975d17514e0eb9092f7',name:'Something',description:'fdgljfgdfjgldfkjglfd'}
console.log(function() {
let on=0, off=0; //define return variables (if there is not 'on' or 'off' element at the object, the amount is 0)
obj['Chapter'].forEach(function(elem) {
if (elem['Citems']) { //if the list item of 'Chapter' does not have a 'Citems' attribute, we will not deal with it
elem['Citems'].forEach(function(e) {
if (e['status'] == 'on') {on = e['time']} else if (e['status'] == 'off') {off = e['time']}
})
}
});
return {
on: on,
off: off
}
}())
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/463607.html
標籤:javascript 数组 目的
上一篇:未處理的例外:型別“List<dynamic>”不是“List<imageObject>”型別的子型別?|如何在顫動的模型firebase中輸入物件陣列
下一篇:將3個陣列轉換為一個物件
