Skip to content Skip to sidebar Skip to footer

How Can I Restructure A Js Object?

I have a list of appointments structured like so (taken from JSON format): var oData = { 'data': { 'd': { 'results': [ {

Solution 1:

You can use reduce -

var newData = oData.data.d.results.reduce(function(prev,curr){
    if (!prev[curr.Room]) { // if room not already in the object add it.
        prev[curr.Room] = [];
    }
    prev[curr.Room].push(curr); 
    return prev;
}, {}); // init with an empty object

console.log(newData);

Post a Comment for "How Can I Restructure A Js Object?"