How to remove the matching element of the array

This question already has an answer here:

  • How do I remove a particular element from an array in JavaScript? 62 answers

  • 找到单词的索引,然后使用拼接将其从阵列中删除。

    var array = ['html', 'css', 'perl', 'c', 'java', 'javascript']  
    var index = array.indexOf('perl');
    
    if (index > -1) {
        array.splice(index, 1);
    }
    

    if you want to just delete the value in the array and leave the spot undefined instead of having that string:

    var arr =['html', 'css', 'perl', 'c', 'java', 'javascript'];
    delete arr[arr.indexOf('perl')];
    

    if you just want to filter that value out:

    var arr2 = arr.filter(function(current,index,array){ return current != "perl"; } );
    

    Just depends on what you want to do with the array and how you want to solve the problem in terms of space and how many times you want to traverse the array.

    链接地址: http://www.djcxy.com/p/3212.html

    上一篇: 从js数组中移除对象知道它是Id

    下一篇: 如何删除数组的匹配元素