Home > Enterprise >  How to recalculate an array of object from nested objects?
How to recalculate an array of object from nested objects?

Time:05-16

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

CodePudding user response:

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

CodePudding user response:

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

  • Related