user2428993
user2428993

Reputation: 323

Reorder JSON based on date using Jquery/JS

Thanks ahead for your help!

I'm trying to reorder some objects so the newest ordered product would be first. So looking at the data below, the new order should be (from newest to oldest) product2, product3, and then product1.

{
    "candyResponse": {
        "product1": {
            "displayName": "Bubble Gum",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod1?$146$",
            "orderDate": {
                "time": "11/03/2018"
            }
        },
        "product2": {
            "displayName": "Chocolate",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod2?$146$",
            "orderDate": {
                "time": "03/05/2015"
            }
        },
        "product3": {
            "displayName": "Mints",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod3?$146$",
            "orderDate": {
                "time": "09/20/2017"
            }
        }
    }
}

I tweaked the code from Sorting an array of JavaScript objects but I was not successful. Tried three ways...

candyResponse.sort(function(a, b) {
   return parseFloat(a.time) - parseFloat(b.time);
});

candyResponse.sort(function(a, b) {
   return parseFloat(a.orderDate) - parseFloat(b.orderDate);
});

candyResponse.sort(function(a, b) {
   return parseFloat(a.orderDate.time) - parseFloat(b.orderDate.time);
});

Thanks again for your help!

Upvotes: 1

Views: 36

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 370819

candyResponse is an object, not an array - objects' property names are not reliably ordered, per the spec, so even if you did create a new object with the properties in the desired insertion order, it wouldn't be something to rely on.

Sort an array instead:

const candyResponse = {
  "product1": {
    "displayName": "Bubble Gum",
    "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod1?$146$",
    "orderDate": {
      "time": "11/03/2018"
    }
  },
  "product2": {
    "displayName": "Chocolate",
    "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod2?$146$",
    "orderDate": {
      "time": "03/05/2015"
    }
  },
  "product3": {
    "displayName": "Mints",
    "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod3?$146$",
    "orderDate": {
      "time": "09/20/2017"
    }
  }
};
const getTime = ([_, product]) => {
  return new Date(product.orderDate.time);
};
const sortedCandyArr = Object.entries(candyResponse)
  .sort((a, b) => getTime(b) - getTime(a));
console.log(sortedCandyArr);

Upvotes: 0

dave
dave

Reputation: 64657

Well, you don't have an array of javascript objects, you just have a javascript object. That being said, you could do:

var data = {
    "candyResponse": {
        "product1": {
            "displayName": "Bubble Gum",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod1?$146$",
            "orderDate": {
                "time": "11/03/2018"
            }
        },
        "product2": {
            "displayName": "Chocolate",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod2?$146$",
            "orderDate": {
                "time": "03/05/2015"
            }
        },
        "product3": {
            "displayName": "Mints",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod3?$146$",
            "orderDate": {
                "time": "09/20/2017"
            }
        }
    }
}

var sorted = Object.keys(data.candyResponse)
            .sort((a, b) => 
                 new Date(data.candyResponse[a].orderDate.time).getTime() - 
                  new Date(data.candyResponse[b].orderDate.time).getTime());
                  
data.candyResponse = sorted.reduce((carry, current) => { carry[current] = data.candyResponse[current]; return carry; }, {});
console.log(data)

Upvotes: -1

Related Questions