• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Removing Items From An Array In Javascript (by index)

Javascript · May 25, 2016

It is that time of the week for Obvious Javascript Tip of the week. You probably already know how to delete an item from an array, maybe not. Sometimes it can be confusing, do I use the delete keyword, do I use slice or do I use splice?

Splice of life

Using splice we can remove items from an array by providing the index (with the first item being zero).

// apple = 0, orange = 1, pineapple = 2, banana = 3
var myArr = ['apple', 'orange', 'pineapple', 'banana'];

myArr.splice(1, 1);

The above example will remove orange from our array (even though oranges are delicious) and the rest of the items will shift and have their indexes adjusted. The first argument is the starting index and the second argument is the number of items to remove.

So we could specify 2 for the second argument and remove both orange and pineapple, but we like pineapple too much to do that. You can also specify one or more optional parameters of items to replace the items removed from the third parameter onwards.

But, I don’t know the index…

Say hello to my little friend indexOf which will let us search the array and return the index if it finds anything. Say you knew banana was in your array, but you didn’t know the index (maybe it just shuffled).

// apple = 0, orange = 1, pineapple = 2, banana = 3
var myArr = ['apple', 'orange', 'pineapple', 'banana'];

var banana = myArr.indexOf('banana');

if (banana != -1) {
    myArr.splice(banana, 1);   
}

You know, having said all of that, it might be worthwhile looking into using a Map or Set if you plan on adding and removing a lot of items, as splice can get pretty expensive for large arrays in my experience.

Dwayne

Leave a Reply Cancel reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
narayana
narayana
5 years ago

myArr.splice();

1
JavaScript Guru
JavaScript Guru
3 years ago

Thanks for sharing Dwayne! This is a great explanation of removing elements from an array using Splice() or the indexOf method.

0

Primary Sidebar

Popular

  • I Joined Truth Social Using a VPN and Editing Some HTML to Bypass the Phone Verification
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript
  • How To Mock uuid In Jest
  • How To Decompile And Compile Android APK's On A Mac Using Apktool
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • Wild Natural Deodorant Review

Recent Comments

  • CJ on Microsoft Modern Wireless Headset Review
  • Dwayne on Microsoft Modern Wireless Headset Review
  • CJ on Microsoft Modern Wireless Headset Review
  • john on Microsoft Modern Wireless Headset Review
  • Dwayne on Why You Should Be Using globalThis Instead of Window In Your Javascript Code

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz