0 points

javascript - Get last element inside new array with key value from Object? - Stack Overflow


dashboard

Fri May 08 2020 07:28:27 GMT+0000 (UTC)

Posted by @sajjadali #javascript

    let data = {"classA":[{"date":"01-01","present":49,"absent":14},{"date":"02-01","present":39,"absent":24},{"date":"03-01","present":35,"absent":28}],"classB":[{"date":"01-01","present":49,"absent":14},{"date":"02-01","present":39,"absent":24},{"date":"03-01","present":35,"absent":28}],"classC":[{"date":"01-01","present":49,"absent":14},{"date":"02-01","present":39,"absent":24},{"date":"03-01","present":35,"absent":28}]}

    let newData = [];

    for (let [key, value] of Object.entries(data)) {
        newData.push({class:key, ...value[value.length - 1]});
    }

    console.log(newData);
                                
content_copy Copy

https://stackoverflow.com/questions/61535303/get-last-element-inside-new-array-with-key-value-from-object