Skip to content Skip to sidebar Skip to footer

How To Add A Key Value Into Pair In Existing Local Storage Object?

I am trying to add a key value pair into an existing localstorage object, below is what the object looks like: category: [{'category_id':1,'name':'Default Category','position':1},{

Solution 1:

You can loop through each object in the array and simply add whatever you want.

let arr = [{
  "category_id": 1,
  "name": "Default Category",
  "position": 1
}, {
  "category_id": 2,
  "name": "Tech",
  "position": 1
}];
var levelCount = 1;
for (let el of arr) {
  el.level = levelCount;
  levelCount++;
}
console.log(arr);

Solution 2:

You have to iterate each object & assign new key, value for it.

var i = 1;
$.each(categoryArray, function (key, item) {
    item.level = i;    
    i++;
});

Now your categoryArray will have new assignment

Post a Comment for "How To Add A Key Value Into Pair In Existing Local Storage Object?"