Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
How to recalculate an array of object from nested objects?

I have an array of objects where Citems is an array of Objects. Each object has status on or off 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'
},{
  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: '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
},
{
on: 120,
off: 60
},
{
on: 120,
off: 60
}]

I tried to use map and reduce in it by not getting the derired result

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You were on the right track using map() and reduce(). An additional call to flatMap() and some destructuring brings it all together:

const result = data.map(({ Chapter }) => Chapter
  .flatMap(({ Citems }) => Citems)
  .reduce((a, { status, time }) => ({
    ...a,
    [status]: (a[status] || 0) + time
  }), {})
);

Complete snippet:

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: '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: 'on',
        time: 60
      }],

    },
    {
      Cname: 'chapter 2',
      Citems: [{
        status: 'on',
        time: 30
      }, {
        status: 'off',
        time: 60
      }]
    }
  ],
  name: 'Something',
  description: 'jfdgljfgdfjgldfkjglfd'
}];

const result = data.map(({ Chapter }) => Chapter
  .flatMap(({ Citems }) => Citems)
  .reduce((a, { status, time }) => ({
    ...a,
    [status]: (a[status] || 0) + time
  }), {})
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

I made it using map and flatMap.

data.map(({ Chapter }) => {
  let on = 0;
  let off = 0;

  Chapter.flatMap(({ Citems }) => Citems).map((item) => {
    if (item.status === "on") {
      on += item.time;
    } else {
      off += item.time;
    }
  });

  return { on, off };
});

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: "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: "on", time: 60 }
        ]
      },
      {
        Cname: "chapter 2",
        Citems: [
          { status: "on", time: 30 },
          { status: "off", time: 60 }
        ]
      }
    ],
    name: "Something",
    description: "jfdgljfgdfjgldfkjglfd"
  }
];

const result = data.map(({ Chapter }) => {
  let on = 0;
  let off = 0;

  Chapter.flatMap(({ Citems }) => Citems).map((item) => {
    if (item.status === "on") {
      on += item.time;
    } else {
      off += item.time;
    }
  });

  return { on, off };
});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!