I have arrayItems on following format:
Remove elements from in Javascript/jQuery
{images: Array(4) [ "abcd.png", "bcd.jpg", "def.jpg", … ]
itemIds: Array(4) [ "1", "3", "2", … ]}
var revid=$(this).attr('data');
Above images and itemIds are stored on storedNames
by parsing JSONfrom localstorage.
revid returns any values which is on itemIds
This is giving me not a function error:
var indexed = storedNames.findIndex(item => item.itemIds === 'revid');
1 ) How can I find the indexof itemIds in storedNames ? (solved)
2) How can I remove all item from object(both array) having index value = indexed For example : I want to remove : abcd.png and 1
?
CodePudding user response:
Thank you @CBroe
I solved this way:
var indexed = storedNames.itemIds.findIndex(item => item === revid);
var newObj = {};
Object.keys(storedNames).forEach(key =>{
newObj[key]=storedNames[key].filter((item,i) => i !=indexToRemove);
});
localStorage.setItem("localstorageItems", JSON.stringify(newObj));
Here localstorageItems is the localstorage object having multiple arrays.