From 6430fcda6f5ff936b509be1a3cd19043d9cc828f Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Thu, 14 Dec 2017 18:49:54 +0530 Subject: [PATCH] Create sqaure_it.md I don't exactly know the use of this snippet but contributors can recommend on this approach of `.reduce()`. --- snippets/sqaure_it.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/sqaure_it.md diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md new file mode 100644 index 000000000..9f25d895e --- /dev/null +++ b/snippets/sqaure_it.md @@ -0,0 +1,18 @@ +### Square The Data + +Pass an array of integers you want to sqaure it +Here using `.reduce()` the new object is accumulator and one by one values from array are passed and go through function. + +``` +const arr = [1,2,3,4,5,6,7]; +arr.reduce(function(a,b){ +a[b] = b * b; +return a; +},{}) // {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} +// +callback accumulator currentValue currentIndex array return value +first call 1 1 0 [0, 1, 2, 3, 4] 1 +second call 2 2 1 [0, 1, 2, 3, 4] 4 +third call 3 3 2 [0, 1, 2, 3, 4] 9 +... +```