Array.reduce() method is one such method. While the
reduce() method might seem confusing at first, it’s definitely one that all developers should have in their toolbox. The
reduce() method takes an array, performs a set of actions on each element of the array, and reduces it to one single value.
The reduce function takes the following syntax:
array.reduce(reducer, initialValue) // the initialValue argument is optional
As shown above, the
reduce() method takes two arguments: a
reducer function and an optional
initial value. Let’s now dive deeper to see what is actually happening under the hood of this
The callback now receives an accumulator (it accumulates all the return values. Its value is the accumulation of a previously returned accumulations), a current value, a current index, and finally, the whole array.
By the word Array methods, I mean the inbuilt array functions, which might be helpful for us in so many ways. So why not just explore and make use of them, to boost our productivity.
Let’s see them together one by one with some amazing examples.
_fill()_method changes all elements in an array to a static value, from a start index (default
_0_) to an end index (default
_array.length_). It returns the modified array.
In simple words, it’s gonna fill the elements of the array with whatever sets of params, you pass in it. Mostly we pass three params, each param stands with some meaning. The first param value: what value you want to fill, second value: start range of index(inclusive), and third value: end range of index(exclusive). Imagine you are going to apply this method on some date, so that how its gonna look like eg: array.fill(‘Some date’, start date, end date).
NOTE: Start range is inclusive and end range is exclusive.
Let’s understand this in the below example-
//declare array var testArray = [2,4,6,8,10,12,14]; console.log(testArray.fill("A"));
When you run this code, you gonna see all the elements of
testArray will be replaced by
array.reduce(callback(currentvalue, index, arr), thisArg)
▶ Vue 2 Basics Beginner Tutorial
▶ Vue.js 2 Components, Beginners tutorial
▶ Vuex tutorial (Vue.js state management)
▶ Vue.js 3 Tutorial
Support my work:
Also, follow us on:
📺 The video in this post was made by Programming with Mosh
The origin of the article: https://www.youtube.com/watch?v=oigfaZ5ApsM&list=PLTjRvDozrdlxEIuOBZkMAK5uiqp8rHUax&index=4
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!
In this video we are going to learn arrays and array methods like sort(), reverse(), join(), split(), pop(), push(), shift(), unshift(), toString(), delete array, etc…