search

Home  >  Q&A  >  body text

javascript - JS deletes elements based on key and returns the length. The difference with splice is that it deletes based on position and the length is not updated.

Like the title, is there such a method?

淡淡烟草味淡淡烟草味2920 days ago568

reply all(2)I'll reply

  • 某草草

    某草草2017-05-18 11:03:17

    The element is deleted, but the original address is still retained, so the subsequent elements must be moved forward. This will reduce the number of elements and update the length

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-18 11:03:17

    Will be updated

    var tt = [1, 2, 3];
    tt.length    // 3
    tt.splice(0, 1);    // [1]
    tt.length    //2

    For your needs, you can write a method yourself and encapsulate splice:

    function my_splice(arr, start, length) {
        arr.splice(start, length);
        return arr.length;
    }

    reply
    0
  • Cancelreply