This article was originally posted on my blog. Head over to inspiredwebdev.com for more articles and tutorials. Check out my JavaScript course on ...
For further actions, you may consider blocking this person and/or reporting abuse
Hey man -- the first section with regards to removing items from an array appears to be incorrect as
Array.prototype.pop()
andArray.prototype.shift()
do opposite of what you say. The comment showing the results also appear to be incorrect. I believe the they should be:Your current code:
Corrected code:
I think it's also worth noting, when introducing these topics, that there are
Array
methods that are mutable and others that are not (return new data structures). A lot of the ones that you've mentioned here mutate the original array. There are some tricky situations that people run into if they aren't aware of these behaviors. I know that you somewhat note that when presentingArray.prototype.filter
, but it would be useful to show that the original array is left unchanged. I mention this because modern JavaScript adopts a lot of functional programming constructs, like immutable data types, that reduce bugs in code (e.g. React, Redux with ImmutableJS). To return a new data structure each time, I typically end up usingArray.prototype.reduce
. This is one of my most used functions or methods in various programming languages.Otherwise, nice write-up! :)
Awesome find, thanks for catching that error. Also thanks for the feedback, I'll try to expand on the explanation of each method :)
This has been very helpful. Especially this part "Replacing Items in an Array of Objects". I am building a project in React and I needed to set the state of an array of objects when one element changes
Awesome! I'm happy it helped
Array.prototype.splice is really cool. I think it's worth to mention that you actually can do a replace with a third argument:
const arr = [1,2,3,4,5];
arr.splice(1, 1, 0);
arr;
// [1,0,3,4,5]
"Array.pop" removes last or first? Please update the post, it's very hard to comprehend which one is which for a newbie like me. I figured out after going through MDN. Thanks for the effort.
You are correct, i should have spellchecked my article more thoroughly. The example was correct, the text was saying the opposite
Array.find will not return a Boolean value in your examples. Instead the value itself will be returned. Or the undefined, if there is no match.
You are perfectly right, i've updated the text
good job ...
I'm annoyed I had to create an account to help you guys out. That's not a knock on you but those who manage this site.
"Ass you can see," search for that in your article. I think you mean "As…"?
That's a nice one, I am late so only have
origianl -> original