鴻蒙傳說
2023-05-19 16:18:40
我有如下代碼let failures: Map<string, Array<string>> = new Map([
['1', ['a', 'b']],
['2', ['c', 'd']],
['3', ['e', 'f']]
])現(xiàn)在讓我有一個(gè)關(guān)鍵值const keyItem = '2'.使用該 keyItem 如何從失敗映射的字符串?dāng)?shù)組中刪除“c”?
1 回答

嗶嗶one
TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
您需要使用failures.get(keyItem)來獲取原始數(shù)組,然后c使用其中一個(gè)刪除Array.prototype.splice()或Array.prototype.filter()然后failures.set(keyItem, modifiedArray)將其寫回 Map
let failures: Map<string, Array<string>> = new Map([
['1', ['a', 'b']],
['2', ['c', 'd']],
['3', ['e', 'f']]
])
const keyItem: string = '2'
const modifiedArray: Array<string> = (failures.get(keyItem) ?? [])
.filter(char => char !== "c")
failures.set(keyItem, modifiedArray)
console.log([...failures])
添加回答
舉報(bào)
0/150
提交
取消