Remove Array Value By index in jquery

前端 未结 5 866
别那么骄傲
别那么骄傲 2020-12-14 15:05

Array:

var arr = {\'abc\',\'def\',\'ghi\'};

I want to remove above array value \'def\' by using index.

5条回答
  •  盖世英雄少女心
    2020-12-14 15:27

    1. Find the element in array and get its position
    2. Remove using the position

    var array = new Array();
      
    array.push('123');
    array.push('456');
    array.push('789');
                     
    var _searchedIndex = $.inArray('456',array);
    alert(_searchedIndex );
    if(_searchedIndex >= 0){
      array.splice(_searchedIndex,1);
      alert(array );
    }

    • inArray() - helps you to find the position.
    • splice() - helps you to remove the element in that position.

提交回复
热议问题