There is a bit of a joke in the land of JavaScript that arrays have one too many functions with scarily similar names. You may have seen this tweet about it:
*googles array splice* damnit, I meant split! *googles array split* damnit, I meant slice! 🤦🏻♀️
So with that introduction, let’s look at what they are and the differences between them!
Splice
Splice can actually do a whole bunch of things. That’s because it takes up to three arguments(that’s not entirely accurate, but you’ll see why in a moment). Let’s start with just the first.
Single Argument
The first argument is an index. If you pass in only that argument, splice will remove all elements in your array after and including the listed index. (In this case you might think of it as a number of elements left in the array, starting from the first).
letcontent=['post','tweet','video','talk']letremoved=content.splice(2)// content is ['post', 'tweet']// removed is ['video', 'talk']
In this example only two elements are left. From the first element in the array, index zero, up until it hit the specified index, two.
Two Arguments
The second argument is the number of elements that you want to remove. This count begins with the index given in the first argument. Let’s look at a couple of examples.
letcontent=['post','tweet','video','talk']letremoved=content.splice(1,2)// content is ['post', 'talk']// removed is ['tweet', 'video']
You can also use negative indices.
letcontent=['post','tweet','video','talk']letremoved=content.splice(-1,1)// content is ['post', 'tweet', 'video']// removed is ['talk']
Three+ Arguments
The final argument(s) are the elements you want to add. These elements will be added starting with the index specified in the first argument. Let’s start with an example in which our second argument is 0, so we aren’t removing anything.
letcontent=['post','tweet','video','talk']content.splice(1,0,'dev')// content is ['post', 'dev', 'tweet', 'video', 'talk']
This copy is a subset of the original. As with splice, slice has multiple arguments, in this case two, and none are required.
No Arguments
If you use slice without any arguments at all it will operate as if you were shallow copying the entire array.
letsocial=['twitter','instagram','facebook','myspace']letsliced=social.slice()// social is ['twitter', 'instagram', 'facebook', 'myspace']// sliced is ['twitter', 'instagram', 'facebook', 'myspace']
Single Argument
The first argument is an index. The copy will start at the given index and include all elements beyond it.
letsocial=['twitter','instagram','facebook','myspace']letsliced=social.slice(1)// social is ['twitter', 'instagram', 'facebook', 'myspace']// sliced is ['instagram', 'facebook', 'myspace']
Note that unlike the splice examples, the original array remains unchanged.
Two Arguments
The second argument is also an index, but it specifies the ending point of the copied array.
letsocial=['twitter','instagram','facebook','myspace']letsliced=social.slice(1,3)// social is ['twitter', 'instagram', 'facebook', 'myspace']// sliced is ['instagram', 'facebook']
Again, social is unchanged. This is always the case when using slice.
Shift
Shift doesn’t take any arguments. It is there to remove the first element of a given array. However, unlike slice, though similar to splice, it manipulates the array directly.
letantisocial=['tv shows','movies','puzzles','board games']letfirstElement=antisocial.shift()// antisocial is ['movies', 'puzzles', 'board games']// firstElement is ['tv shows']
And that’s really all it does!
Split
To be honest, this one isn’t really that similar. However, it’s included in my original tweet, so I’m briefly mentioning it here!
Split takes a string and uses a delimiter to break it up into array elements. Something like this.
letmsg='what sentence can I use for this example'letsplit=msg.split('')// [// 'what', 'sentence',// 'can', 'I',// 'use', 'for',// 'this', 'example'// ]
The reason it might be considered somewhat similar is that if you don’t pass any arguments, you get a copy of the string inside an array.
letmsg='what sentence can I use for this example'letcopy=msg.split()// [ 'what sentence can I use for this example' ]
And that’s the only functionality it has.
Sarah Drasner is here to save us
It’s ok to forget all of this as soon as you’ve read it. Want to know why? The incredible @sarah_edo made an awesome tool called the Array Explorer! Next time you’re looking to manipulate an array and can’t remember what function you want, give it a try.
(function(l) {
if (!l){window.lintrk = function(a,b){window.lintrk.q.push([a,b])};
window.lintrk.q=[]}
var s = document.getElementsByTagName("script")[0];
var b = document.createElement("script");
b.type = "text/javascript";b.async = true;
b.src = "https://snap.licdn.com/li.lms-analytics/insight.min.js";
s.parentNode.insertBefore(b, s);})(window.lintrk);
var astra = {"break_point":"768","isRtl":"","is_scroll_to_id":"","is_scroll_to_top":"1","is_header_footer_builder_active":"1","responsive_cart_click":"flyout","is_dark_palette":"","revealEffectEnable":"","edit_post_url":"https://tenmilesquare.com/wp-admin/post.php?post={{id}}&action=edit","ajax_url":"https://tenmilesquare.com/wp-admin/admin-ajax.php","infinite_count":"2","infinite_total":"0","pagination":"number","infinite_scroll_event":"scroll","no_more_post_message":"No more posts to show.","grid_layout":"1","site_url":"https://tenmilesquare.com","blogArchiveTitleLayout":"","blogArchiveTitleOn":"","show_comments":"Show Comments","masonryEnabled":"","blogMasonryBreakPoint":"0"};
//# sourceURL=astra-theme-js-js-extra