Currently, I am parsing a JSON file which contains a map of JavaScript objects. For instance:
{
offers : {
"1":{"id":"1", "category":"a", "offerType":"LS"},
"2":{"id":"2", "category":"a", "offerType":"EX"},
"3":{"id":"3", "category":"a", "offerType":"EX"},
"4":{"id":"4", "category":"a", "offerType":"LS"}
}
}
After reading this JSON, I am storing it in local storage. My goal is to rearrange it so that offers with an offerType of "LS" appear at the top of my local storage object.
The motivation behind this is to ensure that when I showcase these offers on my website, those with an offerType of "LS" are displayed first. I am implementing this in Angular:
let offers = data.offers;
if (offers != null) {
for (var index in offers) {
var offer = offers[index];
if (offer != undefined) {
if (offer.offerType == 'LS'){
offersLS = [...offersLS, offer];
}
}
}
if (offersLS != null){
offersLS.forEach(offerLS => {
let key = offerLS['id'];
listOffers = offers[key], listOffers;
});
}
listOffers = listOffers, offers;
}
listOffers is the final data that gets saved as my local storage object. I attempted to achieve this with: listOffers = [...offersLS, ...offers] but that approach saves it in my localStorage as an array, whereas I require a 'map' of these objects or an object of objects. I am unsure of the correct terminology.