我想删除数组的元素并调整数组大小。
我用:删除数组中的元素并调整其大小
selectedproducts=jQuery.grep(selectedproducts, function(value) {
return value != thisID;
});
但selectedproducts
的大小保持不变。
我用:
console.log(selectedproducts.length);
后每删除打印selectedproducts
的lenght
,但它不会改变。
请问javascript或jquery有这个功能吗?
编辑:
我有5个元素的数组。
我得到使用Felix控制台什么答案每删除后:
Size:4
["Celery", "Tomatoes", undefined, "Carrots"]
Size:3
["Celery", undefined, "Carrots"]
Size:2
[undefined, "Carrots"]
Size:1
[undefined]
编辑2:
我试图vishakvkt的答案,并能正常工作。
我在控制台中看到什么:
Size:4
["Beans", "Avocado", "Snow Peas", "Tomatoes"]
Size:3
["Avocado", "Snow Peas", "Tomatoes"]
Size:2
["Avocado", "Snow Peas"]
Size:1
["Snow Peas"]
Size:0
[]
'$ .grep'为我工作得很好:HTTP:// jsfiddle.net/J4cVY/。 – 2013-04-28 10:36:25
[可从javascript数组中删除特定元素?](http://stackoverflow.com/questions/5767325/remove-specific-element-from-a-javascript-array) – 2013-04-28 10:38:11