0

I am trying to remove multiple elements from an array by name

This is my code

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);

Could anybody please let me know how to remove multiple elements from a array by name ??

0

2 Answers 2

3

You just need to iterate the 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>

Sign up to request clarification or add additional context in comments.

1 Comment

Please don't forget to accept the answer if it worked for you. Just click the checkmark next to the answer @user3674364
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.