Home > Software engineering >  How to Filter array of object with object and create new object?
How to Filter array of object with object and create new object?

Time:02-16

I have an array of objects

const myData = [
    {
        "paiteint1": [
            {
                "status": "healthy",
                "date": "2022-05-01",
                "hour": "12AM"
            },
            {
                "status": "healthy",
                "date": "2022-05-01",
                "hour": "1AM"
            },
            {
                "status": "need attention",
                "date": "2022-05-01",
                "hour": "2AM"
            },
            {
                "status": "healthy",
                "date": "2022-05-01",
                "hour": "3AM"
            },
            {
                "status": "need attention",
                "date": "2022-05-01",
                "hour": "4AM"
            },
            {
                "status": "critical",
                "date": "2022-05-01",
                "hour": "5AM"
            },
            {
                "status": "healthy",
                "date": "2022-05-01",
                "hour": "6AM"
            },
            {
                "status": "critical",
                "date": "2022-05-01",
                "hour": "7AM"
            },

        ],
        "paiteint2": [
            {
                "status": "healthy",
                "date": "2022-05-01",
                "hour": "12AM"
            },
            {
                "status": "healthy",
                "date": "2022-05-01",
                "hour": "1AM"
            },
            {
                "status": "need attention",
                "date": "2022-05-01",
                "hour": "2AM"
            },
            {
                "status": "healthy",
                "date": "2022-05-01",
                "hour": "3AM"
            },
            {
                "status": "need attention",
                "date": "2022-05-01",
                "hour": "4AM"
            },
            {
                "status": "critical",
                "date": "2022-05-01",
                "hour": "5AM"
            },
            {
                "status": "healthy",
                "date": "2022-05-01",
                "hour": "6AM"
            },
            {
                "status": "critical",
                "date": "2022-05-01",
                "hour": "7AM"
            },
        ]
    }
  ]

and need to convert above json obj into below formate according to the status of his health

required obj

const manipulatedData = [
        {
            "paiteint1 heathy": {
                //all healthy status obj
            },
            "paiteint1 need attention": {
                //all need attention status obj
               
            },
            "paiteint1 critical": 
            {
              //all critical status obj
            },
            "paiteint2 heathy": {
                //all healthy status obj
            },
            "paiteint2 need attention": {
                //all need attention status obj
               
            },
            "paiteint2 critical": 
            {
              //all critical status obj
            },
        }
    ]

I have tried mapping over the obj filter reduce but not able to get the desired o/p. and i am also looking for any other approach if some can suggest that will be great for me.

CodePudding user response:

const myData = [
  {
    paiteint1: [
      {
        status: "healthy",
        date: "2022-05-01",
        hour: "12AM",
      },
      {
        status: "healthy",
        date: "2022-05-01",
        hour: "1AM",
      },
      {
        status: "need attention",
        date: "2022-05-01",
        hour: "2AM",
      },
      {
        status: "healthy",
        date: "2022-05-01",
        hour: "3AM",
      },
      {
        status: "need attention",
        date: "2022-05-01",
        hour: "4AM",
      },
      {
        status: "critical",
        date: "2022-05-01",
        hour: "5AM",
      },
      {
        status: "healthy",
        date: "2022-05-01",
        hour: "6AM",
      },
      {
        status: "critical",
        date: "2022-05-01",
        hour: "7AM",
      },
    ],
    paiteint2: [
      {
        status: "healthy",
        date: "2022-05-01",
        hour: "12AM",
      },
      {
        status: "healthy",
        date: "2022-05-01",
        hour: "1AM",
      },
      {
        status: "need attention",
        date: "2022-05-01",
        hour: "2AM",
      },
      {
        status: "healthy",
        date: "2022-05-01",
        hour: "3AM",
      },
      {
        status: "need attention",
        date: "2022-05-01",
        hour: "4AM",
      },
      {
        status: "critical",
        date: "2022-05-01",
        hour: "5AM",
      },
      {
        status: "healthy",
        date: "2022-05-01",
        hour: "6AM",
      },
      {
        status: "critical",
        date: "2022-05-01",
        hour: "7AM",
      },
    ],
  },
];

const manipulatedData = [];

myData.forEach((d) => {
  Object.keys(d).map((p) => {
    return d[p].map((s) => {
      manipulatedData[`${p} ${s.status}`] = d[p].filter(
        (sp) => sp.status === s.status
      );
    });
  });
});

console.log(manipulatedData);

Result

[
  'paiteint1 healthy': [
    { status: 'healthy', date: '2022-05-01', hour: '12AM' },
    { status: 'healthy', date: '2022-05-01', hour: '1AM' },
    { status: 'healthy', date: '2022-05-01', hour: '3AM' },
    { status: 'healthy', date: '2022-05-01', hour: '6AM' }
  ],
  'paiteint1 need attention': [
    { status: 'need attention', date: '2022-05-01', hour: '2AM' },
    { status: 'need attention', date: '2022-05-01', hour: '4AM' }
  ],
  'paiteint1 critical': [
    { status: 'critical', date: '2022-05-01', hour: '5AM' },
    { status: 'critical', date: '2022-05-01', hour: '7AM' }
  ],
  'paiteint2 healthy': [
    { status: 'healthy', date: '2022-05-01', hour: '12AM' },
    { status: 'healthy', date: '2022-05-01', hour: '1AM' },
    { status: 'healthy', date: '2022-05-01', hour: '3AM' },
    { status: 'healthy', date: '2022-05-01', hour: '6AM' }
  ],
  'paiteint2 need attention': [
    { status: 'need attention', date: '2022-05-01', hour: '2AM' },
    { status: 'need attention', date: '2022-05-01', hour: '4AM' }
  ],
  'paiteint2 critical': [
    { status: 'critical', date: '2022-05-01', hour: '5AM' },
    { status: 'critical', date: '2022-05-01', hour: '7AM' }
  ]
]

CodePudding user response:

const myData = [
  {
    paiteint1: [
      {
        status: 'healthy',
        date: '2022-05-01',
        hour: '12AM',
      },
      {
        status: 'healthy',
        date: '2022-05-01',
        hour: '1AM',
      },
      {
        status: 'need attention',
        date: '2022-05-01',
        hour: '2AM',
      },
      {
        status: 'healthy',
        date: '2022-05-01',
        hour: '3AM',
      },
      {
        status: 'need attention',
        date: '2022-05-01',
        hour: '4AM',
      },
      {
        status: 'critical',
        date: '2022-05-01',
        hour: '5AM',
      },
      {
        status: 'healthy',
        date: '2022-05-01',
        hour: '6AM',
      },
      {
        status: 'critical',
        date: '2022-05-01',
        hour: '7AM',
      },
    ],
    paiteint2: [
      {
        status: 'healthy',
        date: '2022-05-01',
        hour: '12AM',
      },
      {
        status: 'healthy',
        date: '2022-05-01',
        hour: '1AM',
      },
      {
        status: 'need attention',
        date: '2022-05-01',
        hour: '2AM',
      },
      {
        status: 'healthy',
        date: '2022-05-01',
        hour: '3AM',
      },
      {
        status: 'need attention',
        date: '2022-05-01',
        hour: '4AM',
      },
      {
        status: 'critical',
        date: '2022-05-01',
        hour: '5AM',
      },
      {
        status: 'healthy',
        date: '2022-05-01',
        hour: '6AM',
      },
      {
        status: 'critical',
        date: '2022-05-01',
        hour: '7AM',
      },
    ],
  },
]
function convertData(data) {
  const result = [];
  data.forEach((element) => {
    const obj = {};
    const keys = Object.keys(element);
    keys.forEach((key) => {
      const status = element[key].map((item) => {
        return item.status;
      });
      const uniqueStatus = [...new Set(status)];
      uniqueStatus.forEach((status) => {
        obj[key   ' '   status] = element[key].filter((item) => {
          return item.status === status;
        });
      });
    });
    result.push(obj);
  });
  return result;
}
console.log(JSON.stringify(convertData(myData), null, 2));

  • Related