javascript remove item from array, if an item already existing in array

后端 未结 6 664
终归单人心
终归单人心 2021-01-25 23:03

following adds items to array:

var arrayOptions = [];

function AddToFilterOptionList(mode) {
    arrayOptions.push(mode);
    }

remove item fr

6条回答
  •  孤城傲影
    2021-01-25 23:40

    let items = [1, 2, 3, 2, 4, 5, 2, 7];
    let item = 2;
    for (let i = 0; i < items.length; i++) {
        if (items[i] === item) {
            items.splice(i, 1);
            i = i - 1;
        }
    }
    

    If you want to remove the element '2' from items array, it is a way.

提交回复
热议问题