如何从 Jacascript/Jquery 对象中的数组中具有项目索引的对象中删除多个数组?
How to remove multiple array from a oject with index of item from array in Jacascript/Jquery Object?
我有以下格式的数组项:
从 Javascript/jQuery
中删除元素
{images: Array(4) [ "abcd.png", "bcd.jpg", "def.jpg", … ]
itemIds: Array(4) [ "1", "3", "2", … ]}
var revid=$(this).attr('data');
以上图像和 itemIds 通过从本地存储解析 JSON 存储在 storedNames
上。
revid returns itemIds
上的任何值
这不是函数错误:
var indexed = storedNames.findIndex(item => item.itemIds === 'revid');
1 ) 如何在 storedNames 中找到 itemIds 的索引? (已解决)
2) 如何从具有索引 value = indexed 的对象(两个数组)中删除所有项目例如:
我想删除:abcd.png 和 1
?
谢谢@CBroe
我是这样解决的:
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));
此处localstorage Items是具有多个数组的localstorage对象。
我有以下格式的数组项:
从 Javascript/jQuery
中删除元素{images: Array(4) [ "abcd.png", "bcd.jpg", "def.jpg", … ]
itemIds: Array(4) [ "1", "3", "2", … ]}
var revid=$(this).attr('data');
以上图像和 itemIds 通过从本地存储解析 JSON 存储在 storedNames
上。
revid returns itemIds
上的任何值这不是函数错误:
var indexed = storedNames.findIndex(item => item.itemIds === 'revid');
1 ) 如何在 storedNames 中找到 itemIds 的索引? (已解决)
2) 如何从具有索引 value = indexed 的对象(两个数组)中删除所有项目例如: 我想删除:abcd.png 和 1
?
谢谢@CBroe
我是这样解决的:
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));
此处localstorage Items是具有多个数组的localstorage对象。