删除多个键集在地图使用Javascript?



嗨,我有一个由键值对组成的映射。如何删除映射中的多个键集?

地图创建部分

// result key something like 0-119,0-110,0-118 for each entries
this.OptionsMap.set(''+result, selectedList[index]);   


地图删除部分

Const entriesd = JSON.parse(JSON.stringify(selectedList));
for (let [key, value] of Object.entries(entriesd)) 
{
if (value.key === 'AB')
{
console.log('keys',value.index)
this.OptionsMap.delete(''+value.index);

}

}

selectedList

(3) [{…}, {…}, {…}]
0: {id: "101", value: "A1B", key: "AB", index: "0-119", …}
1: {id: "130", value: "130", key: "AB", index: "0-110", …}
2: {id: "130", value: "130", key: "AB", index: "0-118", …}

现在在console.log我可以得到我的索引像0-119,0-110,0-118,但在删除部分如何删除多个索引?请帮帮我。提前感谢。

怎么样?

// SETTING AN EXAMPLE FOR THE CASE
var selected = [
{id: "101", value: "A1B", key: "AA", index: "0-119"},
{id: "130", value: "130", key: "AB", index: "0-110"},
{id: "130", value: "130", key: "AB", index: "0-118"}
];
var optionsMap = new Map([
["0-119", {id: "101", value: "A1B", key: "AA", index: "0-119"}],
["0-110", {id: "130", value: "130", key: "AB", index: "0-110"}],
["0-118", {id: "130", value: "130", key: "AB", index: "0-118"}],
["0-117", {id: "135", value: "131", key: "AB", index: "0-117"}],
["0-116", {id: "136", value: "132", key: "AB", index: "0-116"}],
]);
console.log('optionsMap.size',optionsMap.size);
// ACTUAL FILTERING
// THIS WILL DELETE ONLY THE OPTIONS BEING SPECIFIED IN selected
// AND HAVING KEY 'AB'
selected.filter(item => item.key === 'AB').forEach(el => optionsMap.delete(el.index));
// RESULT
console.log('optionsMap.size',optionsMap.size);

最新更新