javascript : delete object from array

dear experts
I have an array of 10 Objects

var myData : Array[10]
0: Object
id : 1
name: jay
dept: cs

1: Object
id : 2
name: jay
dept: cs


2: Object
id : 3
name: sam
dept: csp

I am trying to delete all the objects in array with name: jay
for(i = 0; i < myData.length; i++){
  if('jay' == myData[i].name){
    myData.splice(i, 1);  //is this correct ? it seems to create a new array
  }
}

Open in new window


Any idea is splice should be used here?

thanks
royjaydAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
That is correct.  .splice is the way to do it.
0
royjaydAuthor Commented:
I've requested that this question be deleted for the following reason:

found the solution
0
Julian HansenCommented:
The other option is to create a new array with the items you want to keep
var newData = new Array();
for(i = 0; i < myData.length; i++){
  if('jay' == myData[i].name){
     newData.push(myData[i]); 
  }
}

Open in new window

I ran some tests on this vs the .splice method - worked out to be a bout 6 times faster for a 1000 iterations through an array with 660 items.
The .splice method took about 86ms vs 12ms for the .push

Much of a muchness really.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Julian HansenCommented:
A valid answer was posted to the question.
0
royjaydAuthor Commented:
>>That is correct.  .splice is the way to do it.

We need to decrement the index, otherwise splice alone will not work.

 
myData.splice(i, 1);  
i--
0
Julian HansenCommented:
Thank you for posting back. The reason for my objection was  that the original question was whether or not .splice was the correct way of removing the element from the array and not about the correctness of the implementation. There was nothing in the post to indicate you were not getting correct results only about whether the use of splice was the correct way to go.

In my test code I do decrement - I did not notice that your code did not but was focusing on the substance of the question itself - hence the post regarding the comparative performance overhead of .splice vs creating a new array.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.