如何从嵌套对象重新计算对象?

发布于 2025-01-23 12:52:30 字数 472 浏览 0 评论 0原文

我有一个对象,citem是一个对象。每个对象都具有或时间和时间。

{
  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
}

我在地图上尝试的,并减少但感到困惑。

I have an object where Citems is an array of Object. Each object has status on or of and 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'
}

I want to generate an array or object from it that show total time for each status like below

{
on: 120,
off: 60
}

I tried with map and reduce but getting confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇柔作态 2025-01-30 12:52:30

您只需要一个嵌套的“总和”,此处使用 redair() 并利用计算属性使用status status作为键更新累加器。

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。 .. 循环

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);

要将其扩展到此类对象的数组,您可以将其再次嵌套在redaim()中。

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);

You just need a nested 'sum', here implemented using reduce() and making use of computed properties to update the accumulator using the status as key.

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);

Or using a for...of loop

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);

To extend this to an array of such Chapter objects you could nest it once more in a 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);

痴者 2025-01-30 12:52:30
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
  }
}())

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
  }
}())

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文