1. 程式人生 > 其它 >JS 根據值刪除陣列包含值的對應項

JS 根據值刪除陣列包含值的對應項

技術標籤:JavaScriptjavascript前端

需求
根據ID刪除陣列中包含此ID的這一項。

去除陣列 a 中 id=1 的物件

let a = [{ id: 1 , name: 1 }, { id: 2 , name: 2 }, { id: 3 , name: 3 }, { id: 4 , name: 4 }];
let b = [1];

a.filter(item => { return !b.includes(item.id); })

結果

includes() 方法用於判斷字串是否包含指定的子字串—返回的是布林值

語法:string.includes(searchvalue, start)
searchvalue:必需,要查詢的字串。
start:可選,設定從那個位置開始查詢,預設為 0。

let str = 'abcd';
let flag = str.includes('e');
console.log(flag);  // false