With array often we need to find the position of an item (index).
For the primitive values we use IndexOf() method.
For function, array and object we use findIndex() .We will also se this later.
If we want to add an item in the beginning of an array we do this:
If we want to add more than one item we do this:
We add the other items separated by comma.
Sometimes we have to remove the first item of an array , we can do this in this way:
If we want remove more than one item in the beginning we do this:
In the code above we use shift() method two times to delete two items.
Now we talk about to add element and remove element in the end of an array.
If we want to add an item in the end of an array we can do this:
Now more than one item:
To delete the last item of an array we may do this:
More than one:
There is many occasions when we want to create a copy of our array to avoid changes in the original, we may perform this with slice() method.
In the end I want to talk about splice() that allow us to add, remove item / items from the beginning, the end or in the middle of an array. Is a penknife method.
Anatomy of splice() method:
Add one or more items in the beginnig of an array:
More than one:
Remove one from the beginning:
Remove more than one:
Add one item in the end:
Add more than one item in the end:
The last thing that I want to show you is how to add or remove Items from the middle of an array with the splice() method:
Add:
Remove:
And replace:
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)