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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.
royjaydAuthor Commented:
I've requested that this question be deleted for the following reason:

found the solution
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){

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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Julian HansenCommented:
A valid answer was posted to the question.
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);  
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.

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

From novice to tech pro — start learning today.