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

后端 未结 6 665
终归单人心
终归单人心 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:44

    This will work assuming the 'this+that' pattern is consistent, and that we only care about the starting item.

    http://jsbin.com/gefasuqinu/1/edit?js,console

    var arr = [];
    
    function remove(item) {
      var f = item.split('+')[0];
    
      for (var i = 0, e = arr.length; i < e; i++) {
        if (arr[i].split('+')[0] === f) {
          arr.splice(i, 1);
          break;
        }
      }
    }
    
    function add(item) {
      remove(item);
      arr.push(item);
    }
    

提交回复
热议问题