最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery remove array elements if the element value starts with the specified string - Stack Overflow

programmeradmin2浏览0评论

I have a javascript array like

["1.A","1.B","1.C","1.D",...,"2.A","2.B",...]

Now I want to remove elements from this array by a specified string. For exm if I specify "1" then it should remove all of "1.A","1.B","1.C","1.D"....How to do this in jQuery.

I have a javascript array like

["1.A","1.B","1.C","1.D",...,"2.A","2.B",...]

Now I want to remove elements from this array by a specified string. For exm if I specify "1" then it should remove all of "1.A","1.B","1.C","1.D"....How to do this in jQuery.

Share Improve this question edited Aug 9, 2012 at 16:10 j08691 208k32 gold badges269 silver badges280 bronze badges asked Aug 9, 2012 at 16:08 Hector BarbossaHector Barbossa 5,52813 gold badges50 silver badges70 bronze badges 4
  • as has already been stated, the easiest solution is a map or filter function which removes the offending elements from your array – jackwanders Commented Aug 9, 2012 at 16:18
  • @jackwanders: Neither of them remove anything from the original Array. They both create a new Array based on the return value. This can be an issue if there are other references to the original that rely on the modifications. – user1106925 Commented Aug 9, 2012 at 16:20
  • @amnotiam arr = arr.filter(...) or arr = $.map(arr,function(){...}). Just reassign the array to the return value of the function. – jackwanders Commented Aug 9, 2012 at 16:24
  • @jackwanders: They reassign the return value to the variable. In JavaScript, arrays are reference types. Any other variables that hold a reference to the original Array will not see the updates. jsfiddle DEMO – user1106925 Commented Aug 9, 2012 at 16:26
Add a ment  | 

5 Answers 5

Reset to default 11

You could use the native .filter() function:

var array = ["1.A","1.B","1.C","1.D",...,"2.A","2.B",...];
array = array.filter(function(value) {
    return value.indexOf('1') < 0;
});

or with jQuery use the .grep() method:

var array = $.grep(array, function(value) {
    return value.indexOf('1') < 0;
});

You don't need jQuery to do this--regular JavaScript will do just fine. The code loops through aArray, looks at the first character of the array element with .substring, checks to see if it's equal to your specified prefix (in this case 1) and if it matches, it uses .splice to remove that element from the array:

var aArray = ["1.A","1.B","1.C","1.D","2.B","2.C"];

var i = aArray.length;
while(i--) {  
    if ((aArray[i].substring(0,1) == "1") {
       var oRemoved = aArray.splice(i,1);
    }

}

You can use the jQuery map() method to do this:

var filter = '1';
origArr = $.map(origArr, function (value, index) {
    if($.inArray(filter, value) != 0) {
        return value;
    }
});

You don't need jQuery to do this.

var data = ["1.A", "1.B", "1.C", "1.D", "2.A", "2.B"];

function removeElements(str) {
    return data.filter(function(a) {
        return (a.indexOf(str + ".") < 0);
    });
}

alert(removeElements(1));
alert(removeElements(2));

You seem to want a function that destructively modifies the original array. Here's one:

Array.prototype.removeElementsWithPrefix = function(prefix) {
  for (var i = this.length - 1; i >= 0; i--) {
    if (this[i].indexOf(prefix) == 0) {
      this.splice(i, 1);
    }
  }
  return this;
}


var a = ["foo", "bar", "baz"];
a.removeElementsWithPrefix("b");
a; // => ["foo"]

I don't actually remend coding a destructive function like this however since a function that copies the original array is safer and more flexible.

发布评论

评论列表(0)

  1. 暂无评论