如何按名称从数组中删除多个元素

时间:2014-09-24 12:45:42

标签: jquery

我正在尝试按名称从数组中删除多个元素

这是我的代码

var mularr = ["jQuery","JavaScript","HTML","Ajax","Css"];
var itemstoRemove = ["jQuery","HTML"];
for(var i=0;i<itemstoRemove.length;i++)
{
   mularr.splice($.inArray(itemstoRemove, mularr),1);
}
alert(mularr);

有人可以告诉我如何通过名称从数组中删除多个元素吗?

2 个答案:

答案 0 :(得分:3)

您只需要迭代itemsToRemove -

var mularr = ["jQuery","JavaScript","HTML","Ajax","Css"];
var itemstoRemove = ["jQuery","HTML"];
for(var i=0;i<itemstoRemove.length;i++)
{
   mularr.splice($.inArray(itemstoRemove[i], mularr),1); // note the [i]
}
console.log(mularr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

答案 1 :(得分:1)

var main = [1,2,3,4,5,1,2,3,9];   // main array                                                              
var removedItemsIndex = [0,2,4];  // array of index to delete of main array
var result=[];
for(var i=0;i<main.length;i++){
    if(removedItemsIndex.indexOf(i)==-1)
    result.push(main[i])
}
console.log(result)