search

Home  >  Q&A  >  body text

javascript - How to remove several values ​​​​in an array and only provide the subscript of the array

Splice removal will always replace the array and subscript, so the subscript I remembered before is useless.

巴扎黑巴扎黑2713 days ago936

reply all(5)I'll reply

  • 阿神

    阿神2017-06-30 10:01:33

    Delete the ones with big subscripts first and then delete the ones with small subscripts

    reply
    0
  • 代言

    代言2017-06-30 10:01:33

    Replace the array with an object, use the delete method to delete, and the subscript will not change

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-30 10:01:33

    The map method of the array removes the elements at the specified index position and generates a new array

    Or direct filter method

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-30 10:01:33

    You can try it, create a new array, and then loop through the array you want to modify. If the subscript is not the number you want to delete, then push the element at this position into your new array. If The subscript is the number you want to delete, just continue to jump out of the loop
    In this way, after the loop ends, the new array is the array you need, and then assign it to the old array

    reply
    0
  • 学习ing

    学习ing2017-06-30 10:01:33

    To generate a new array:

    arr = [1,2,3,4,5,6,7]
    removes = [1,3,5]
    arr = arr.filter(function(value, index) { 
        return removes.indexOf(index) < 0
    });

    Do not generate new array:

    arr = [1,2,3,4,5,6,7]
    removes = [1,3,5]
    Array.prototype.remove = function(removes){
        removes.sort(function(a, b) {
            return a - b;
        }).reverse().forEach(function(value){this.splice(value, 1)
       }.bind(this)
    )};
    arr.remove(removes)

    reply
    0
  • Cancelreply