JavaScript apply() - javascript

When I pass an array and Object respectively to function.apply(), I get the o/p of NaN but when I do it Object and array, I get a Number. Why is this happening?
since arrays are also considered as objects, why I'm unable to use this to represent array from the same context/
I've tried changing the position of variable calling but I know for a fact that the order only matters when it's a parameter.
function add() {
return arr[0] + arr[1] + this.a + this.b;
}
let obj = {
a: 1,
b: 2
};
let arr = [1, 2];
console.log(add.apply(arr, obj));
console.log(add.apply(obj, arr));
O/P
NaN
6

Perhaps a diagram could help. Your function uses no parameters, so what you supply in the second argument to apply (which should be an array of values) doesn't matter. Only the first parameter (which becomes this in the function body) matters.
When you call with arr, then this points to the arr supplied in the argument, but arr also points to the same object, as it's global and not overwritten anywhere:
function add() {
// 1 + 1 + undefined + undefined //=> NaN
return arr[0] + arr[1] + this.a + this.b;
} // | | | |
// +------+--------+---------+-----------+
// |
// V
let arr = [1, 2]; let obj = {a: 1, b: 2};
// ^
// `-----Nothing points to this
add.apply(arr) // equivalent to `add.apply(arr, obj)`, since `add` ignores parameters
When you call with obj, then this points to the obj supplied in the argument, and arr again points to the global arr object:
function add() {
// 1 + 2 + 1 + 2 //=> 6
return arr[0] + arr[1] + this.a + this.b;
} // | | | |
// +------+--------+ +-----------+
// | |
// V V
let arr = [1, 2]; let obj = {a: 1, b: 2};
add.apply(obj) // equivalent to `add.apply(obj, arr)`, since `add` ignores parameters

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
function.apply(thisArg, [argsArray]) your first argument becomes this in the function, and second argument is an array of arguments youre passing to the function
so add.apply(arr, obj) really should be add.apply(arr, [obj]) would translate to the following, which this(arr) doesnt have properties a nor b
function add() {
return arr[0] + arr[1] + arr.a + arr.b;
}
// then invoking it as
add(obj);
and add.apply(obj, arr) would translate to
function add() {
return arr[0] + arr[1] + obj.a + obj.b;
}
// them invoking it as
add(1, 2);

Related

How to "splice" an object?

Here is what I want to achieve:
A function called Splice which can take 3 arguments:
The first is an object.
The second is numeric.
The third is numeric.
It takes properties from an object and uses them for a newly created object.
Which properties are defined by the two numbers. The second argument indicates the position of the key/value pair that I want to start taking properties from and the third indicates how many I want to remove.
So for instance splice(0,2) refers to the positions 0 and 1 of the object having the following object: var obj = {a: 1, b: 2, c: 2} and calling the function splice with the arguments obj, 0, 2 should return {a: 1, b: 2}
If the third argument is not passed it should default to 1.
This is what I have so far (not yet respecting the last part when only one number is passed in):
function splice(object, number, number2) {
var newOjb = {};
var count = 0;
object.forEach(function(element, index) {
count++;
if(count <= number2 && count > number) {
newObj[key]=item
} else if (number == count) {
newObj[key]=item
}
})
return newObj;
}
However, I seem to use some wrong syntax as my console tells me my forEach loop is not a function and the code, in general, doesn't seem to work.
Any ideas for basic solutions (no fancy techniques)?
Use Object.entries(object).forEach(function([key, item], index) { (Note: This will not guarantee order). You have also typo in var newOjb = {};
P.S. You don't need that count, you can use index in your conditions
number - index, number2 - count
function splice(object, number, number2) {
var newObj = {};
if (!number2) {
number2 = 1;
}
Object.entries(object).forEach(function([key, item], index) {
if (index >= number && index - number < number2) {
newObj[key] = item
}
})
return newObj;
}
let obj = {
a: 1,
b: 2,
c: 2
};
console.log(splice(obj, 0, 2))
console.log(splice(obj, 2))
console.log(splice(obj, 1, 2))
FWIW, here's a function that behaves exactly like Array.splice for objects (don't know if this qualifies as "fancy" though):
function spliceObject(obj, start, deleteCount, ...items) {
let e = Object.entries(obj);
let r = e.splice(start, deleteCount, ...items.flatMap(Object.entries));
Object.keys(obj).forEach(k => delete obj[k]);
e.forEach(([k, v]) => obj[k] = v);
return Object.fromEntries(r);
}
//
obj = {a:1, b:2, c:3, d:4, e:5, f:6 }
ret = spliceObject(obj, 1, 3, {xyz:88}, {blah:99})
console.log(obj)
console.log(ret)
Do note however that you generally shouldn't rely on object properties being ordered in any particular way. The ordering is defined and mandated by the standard, but not all engines and tools get it right.
For your default value question, this is a pattern that works well
function myfunction1(val) {
this.val = val ? val : 1;
console.log(this.val);
}

JS HELP to understand Reduce((countsMap, item) => countsMap.set(item, countsMap.get(item) + 1 || 1), new Map))

I had a task and I found a perfect solution, though i don't really understand how that works. Can anyone help me to explain that in details? Especially this one (countsMap, item) => countsMap.set(item, countsMap.get(item) + 1 || 1)
var testArray = ["dog", "dog", "cat", "buffalo", "wolf", "cat", "tiger", "cat"];
function compressArray(original) {
return array.reduce((countsMap, item) => countsMap.set(item, countsMap.get(item) + 1 || 1), new Map());
}
console.log(compressArray(testArray));
Lets break it down into the constituent parts
const foo = countsMap.get(item); // get the value of key `item` from a Map
const bar = foo + 1 || 1; // Increment foo by 1, or start at 1
// n.b. this works because `undefined + 1; // NaN`, I would instead
// recommend `(foo || 0) + 1`
const baz = countsMap.set(item, bar); // set the value of key `item` to `bar`
// n.b. this is chainable (i.e. returns `countsMap`)
const fn = (x, y) => x + y; // a function which takes x and y, and returns their sum
// so finally,
const reducor = (countsMap, item) => countsMap.set(item, countsMap.get(item) + 1 || 1);
// 1. look up from map
// 2. increment 1, or start at 1
// 3. set back to map
// 4. return map
Array.prototype.reduce
I find the easiest way to express this to someone is to write it longhand
const arr = [1, 2, 3]; // some array
const sum = (accumilator, nextValue) => accumilator + nextValue; // some function
// n.b. the return value is the next accumilator
const initialValue = 0;
const result = arr.reduce(sum, initialValue); // 6, i.e. 1 + 2 + 3
So what does this look like longhand, e.g. in a for loop?
// lets start the same as before
const arr = [1, 2, 3];
const sum = (accumilator, nextValue) => accumilator + nextValue;
// now to calculate the result,
let accumilator = 0; // = initial value
for (let i = 0; i < arr.length; ++i) {
const nextValue = arr[i];
accumilator = sum(accumilator, nextValue);
}
accumilator; // 6
The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
For the => called Arrow functions, it's works like
(param1, param2, …, paramN) => { statements }
var materials = ['Hydrogen', 'Helium', 'Lithium', 'Beryllium'];
console.log(materials.map((material) => {
return material.length;
})); // [8, 6, 7, 9]
You use a loop (but fancy - reduce) over an array testArray.
While doing a loop you use an object (but fancy - Map) to reflect how many times values are noticed in array.
You use also arrow function, that's why syntax is concise and you don't need to write return statement.

JavaScript closure parameters

foo is equal to iCantThinkOfAName, but the iCantThinkOfAName has two parameters, and foo only one. I don’t understand how foo(2) returns num: 4.
function iCantThinkOfAName(num, obj) {
// This array variable, along with the 2 parameters passed in,
// are 'captured' by the nested function 'doSomething'
var array = [1, 2, 3];
function doSomething(i) {
num += i;
array.push(num);
console.log('num: ' + num);
console.log('array: ' + array);
console.log('obj.value: ' + obj.value);
}
return doSomething;
}
var referenceObject = {
value: 10
};
var foo = iCantThinkOfAName(2, referenceObject); // closure #1
var bar = iCantThinkOfAName(6, referenceObject); // closure #2
foo(2);
/*
num: 4
array: 1,2,3,4
obj.value: 10
*/
foo is not equal to iCantThinkOfAName, it is equal to iCantThinkOfAName(2, referenceObject) which returns the inner function doSomething within a closure containing num (which is equal to the 2 you passed in), obj which is your referenceObject you passed in, and array = [1, 2, 3];.
When you call foo(2) you are directly calling that inner doSomething where i is your 2 you are passing in, which gets added to your num which was your original 2; thus, num within the closure, is now 4.

How to change a function accepted arguments after creating it on Javascript?

Or, in other words, how to make this work:
function foo(){}
//do something that modifies foo as if it was defined with "function foo(a,b,c){};"
console.log(foo.length);
//output: 3
It is possible, but maybe not very nice:
function lengthDecorator(fun) {
function update(len) {
var args = []; // array of parameter names
for (var i = 0; i < len; ++i) {
args.push('a' + i);
}
var result = new Function('fun',
'return function(' + args.join(',') + ') {' +
'var args = Array.prototype.slice.call(arguments);' +
'return fun.apply(this, args);' + // call supplied function
'}'
); // create a function that will return a function
result = result(fun); // make the fun param known to the inner function
result.update = update;
return result;
}
return update(fun.length);
}
Example usage:
var foo = lengthDecorator(function(a,b) {
return a+b;
});
print('foo.length: ' + foo.length);
print('foo(2, 3): ' + foo(2, 3));
print('');
foo = foo.update(42);
print('foo.length: ' + foo.length);
print('foo(2, 3): ' + foo(2, 3));
Output:
foo.length: 2
foo(2, 3): 5
foo.length: 42
foo(2, 3): 5
(Live demo: Ideone.com, jsFiddle)
lengthDecorator wraps the supplied function with a function that takes the same amount of parameters as the supplied function. The parameter count can be changed with update.
C.f.
new Function(...): Dynamically create a new function.
fun.apply(...): "Calls a function with a given this value and arguments provided as an array."
function foo() {}
alert(foo.length); // 0
foo = function (a, b, c) {}
alert(foo.length); // 3
I'm not sure what you're actually trying to do, but you can store the old foo in var and then redefine foo.
function foo() {...}
var oldfoo = foo;
foo = function (a, b, c) {
oldfoo();
}
But what's the point?
The length property of a function object is non-writable and non-configurable, so there is no way to change its value.
You could define a new function which invokes the original function internally...

How to call reduce on an array of objects to sum their properties?

Say I want to sum a.x for each element in arr.
arr = [ { x: 1 }, { x: 2 }, { x: 4 } ];
arr.reduce(function(a, b){ return a.x + b.x; }); // => NaN
I have cause to believe that a.x is undefined at some point.
The following works fine
arr = [ 1, 2, 4 ];
arr.reduce(function(a, b){ return a + b; }); // => 7
What am I doing wrong in the first example?
A cleaner way to accomplish this is by providing an initial value as the second argument to reduce:
var arr = [{x:1}, {x:2}, {x:4}];
var result = arr.reduce(function (acc, obj) { return acc + obj.x; }, 0);
console.log(result); // 7
The first time the anonymous function is called, it gets called with (0, {x: 1}) and returns 0 + 1 = 1. The next time, it gets called with (1, {x: 2}) and returns 1 + 2 = 3. It's then called with (3, {x: 4}), finally returning 7.
This also handles the case where the array is empty, returning 0.
After the first iteration your're returning a number and then trying to get property x of it to add to the next object which is undefined and maths involving undefined results in NaN.
try returning an object contain an x property with the sum of the x properties of the parameters:
var arr = [{x:1},{x:2},{x:4}];
arr.reduce(function (a, b) {
return {x: a.x + b.x}; // returns object with property x
})
// ES6
arr.reduce((a, b) => ({x: a.x + b.x}));
// -> {x: 7}
Explanation added from comments:
The return value of each iteration of [].reduce used as the a variable in the next iteration.
Iteration 1: a = {x:1}, b = {x:2}, {x: 3} assigned to a in Iteration 2
Iteration 2: a = {x:3}, b = {x:4}.
The problem with your example is that you're returning a number literal.
function (a, b) {
return a.x + b.x; // returns number literal
}
Iteration 1: a = {x:1}, b = {x:2}, // returns 3 as a in next iteration
Iteration 2: a = 3, b = {x:2} returns NaN
A number literal 3 does not (typically) have a property called x so it's undefined and undefined + b.x returns NaN and NaN + <anything> is always NaN
Clarification: I prefer my method over the other top answer in this thread as I disagree with the idea that passing an optional parameter to reduce with a magic number to get out a number primitive is cleaner. It may result in fewer lines written but imo it is less readable.
TL;DR, set the initial value
Using destructuring
arr.reduce( ( sum, { x } ) => sum + x , 0)
Without destructuring
arr.reduce( ( sum , cur ) => sum + cur.x , 0)
With Typescript
arr.reduce( ( sum, { x } : { x: number } ) => sum + x , 0)
Let's try the destructuring method:
const arr = [ { x: 1 }, { x: 2 }, { x: 4 } ]
const result = arr.reduce( ( sum, { x } ) => sum + x , 0)
console.log( result ) // 7
The key to this is setting initial value. The return value becomes first parameter of the next iteration.
Technique used in top answer is not idiomatic
The accepted answer proposes NOT passing the "optional" value. This is wrong, as the idiomatic way is that the second parameter always be included. Why? Three reasons:
1. Dangerous
-- Not passing in the initial value is dangerous and can create side-effects and mutations if the callback function is careless.
Behold
const badCallback = (a,i) => Object.assign(a,i)
const foo = [ { a: 1 }, { b: 2 }, { c: 3 } ]
const bar = foo.reduce( badCallback ) // bad use of Object.assign
// Look, we've tampered with the original array
foo // [ { a: 1, b: 2, c: 3 }, { b: 2 }, { c: 3 } ]
If however we had done it this way, with the initial value:
const bar = foo.reduce( badCallback, {})
// foo is still OK
foo // { a: 1, b: 2, c: 3 }
For the record, unless you intend to mutate the original object, set the first parameter of Object.assign to an empty object. Like this: Object.assign({}, a, b, c).
2 - Better Type Inference
--When using a tool like Typescript or an editor like VS Code, you get the benefit of telling the compiler the initial and it can catch errors if you're doing it wrong. If you don't set the initial value, in many situations it might not be able to guess and you could end up with creepy runtime errors.
3 - Respect the Functors
-- JavaScript shines best when its inner functional child is unleashed. In the functional world, there is a standard on how you "fold" or reduce an array. When you fold or apply a catamorphism to the array, you take the values of that array to construct a new type. You need to communicate the resulting type--you should do this even if the final type is that of the values in the array, another array, or any other type.
Let's think about it another way. In JavaScript, functions can be pass around like data, this is how callbacks work, what is the result of the following code?
[1,2,3].reduce(callback)
Will it return an number? An object? This makes it clearer
[1,2,3].reduce(callback,0)
Read more on the functional programming spec here: https://github.com/fantasyland/fantasy-land#foldable
Some more background
The reduce method takes two parameters,
Array.prototype.reduce( callback, initialItem )
The callback function takes the following parameters
(accumulator, itemInArray, indexInArray, entireArray) => { /* do stuff */ }
For the first iteration,
If initialItem is provided, the reduce function passes the initialItem as the accumulator and the first item of the array as the itemInArray.
If initialItem is not provided, the reduce function passes the first item in the array as the initialItem and the second item in the array as itemInArray which can be confusing behavior.
I teach and recommend always setting the initial value of reduce.
You can check out the documentation at:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Others have answered this question, but I thought I'd toss in another approach. Rather than go directly to summing a.x, you can combine a map (from a.x to x) and reduce (to add the x's):
arr = [{x:1},{x:2},{x:4}]
arr.map(function(a) {return a.x;})
.reduce(function(a,b) {return a + b;});
Admittedly, it's probably going to be slightly slower, but I thought it worth mentioning it as an option.
To formalize what has been pointed out, a reducer is a catamorphism which takes two arguments which may be the same type by coincidence, and returns a type which matches the first argument.
function reducer (accumulator: X, currentValue: Y): X { }
That means that the body of the reducer needs to be about converting currentValue and the current value of the accumulator to the value of the new accumulator.
This works in a straightforward way, when adding, because the accumulator and the element values both happen to be the same type (but serve different purposes).
[1, 2, 3].reduce((x, y) => x + y);
This just works because they're all numbers.
[{ age: 5 }, { age: 2 }, { age: 8 }]
.reduce((total, thing) => total + thing.age, 0);
Now we're giving a starting value to the aggregator. The starting value should be the type that you expect the aggregator to be (the type you expect to come out as the final value), in the vast majority of cases.
While you aren't forced to do this (and shouldn't be), it's important to keep in mind.
Once you know that, you can write meaningful reductions for other n:1 relationship problems.
Removing repeated words:
const skipIfAlreadyFound = (words, word) => words.includes(word)
? words
: words.concat(word);
const deduplicatedWords = aBunchOfWords.reduce(skipIfAlreadyFound, []);
Providing a count of all words found:
const incrementWordCount = (counts, word) => {
counts[word] = (counts[word] || 0) + 1;
return counts;
};
const wordCounts = words.reduce(incrementWordCount, { });
Reducing an array of arrays, to a single flat array:
const concat = (a, b) => a.concat(b);
const numbers = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
].reduce(concat, []);
Any time you're looking to go from an array of things, to a single value that doesn't match a 1:1, reduce is something you might consider.
In fact, map and filter can both be implemented as reductions:
const map = (transform, array) =>
array.reduce((list, el) => list.concat(transform(el)), []);
const filter = (predicate, array) => array.reduce(
(list, el) => predicate(el) ? list.concat(el) : list,
[]
);
I hope this provides some further context for how to use reduce.
The one addition to this, which I haven't broken into yet, is when there is an expectation that the input and output types are specifically meant to be dynamic, because the array elements are functions:
const compose = (...fns) => x =>
fns.reduceRight((x, f) => f(x), x);
const hgfx = h(g(f(x)));
const hgf = compose(h, g, f);
const hgfy = hgf(y);
const hgfz = hgf(z);
For the first iteration 'a' will be the first object in the array, hence a.x + b.x will return 1+2 i.e. 3.
Now in the next iteration the returned 3 is assigned to a, so a is a number now n calling a.x will give NaN.
Simple solution is first mapping the numbers in array and then reducing them as below:
arr.map(a=>a.x).reduce(function(a,b){return a+b})
here arr.map(a=>a.x) will provide an array of numbers [1,2,4] now using .reduce(function(a,b){return a+b}) will simple add these numbers without any hassel
Another simple solution is just to provide an initial sum as zero by assigning 0 to a as below:
arr.reduce(function(a,b){return a + b.x},0)
At each step of your reduce, you aren't returning a new {x:???} object. So you either need to do:
arr = [{x:1},{x:2},{x:4}]
arr.reduce(function(a,b){return a + b.x})
or you need to do
arr = [{x:1},{x:2},{x:4}]
arr.reduce(function(a,b){return {x: a.x + b.x}; })
If you have a complex object with a lot of data, like an array of objects, you can take a step by step approach to solve this.
For e.g:
const myArray = [{ id: 1, value: 10}, { id: 2, value: 20}];
First, you should map your array into a new array of your interest, it could be a new array of values in this example.
const values = myArray.map(obj => obj.value);
This call back function will return a new array containing only values from the original array and store it on values const. Now your values const is an array like this:
values = [10, 20];
And now your are ready to perform your reduce:
const sum = values.reduce((accumulator, currentValue) => { return accumulator + currentValue; } , 0);
As you can see, the reduce method executes the call back function multiple times. For each time, it takes the current value of the item in the array and sum with the accumulator. So to properly sum it you need to set the initial value of your accumulator as the second argument of the reduce method.
Now you have your new const sum with the value of 30.
I did it in ES6 with a little improvement:
arr.reduce((a, b) => ({x: a.x + b.x})).x
return number
In the first step, it will work fine as the value of a will be 1 and that of b will be 2 but as 2+1 will be returned and in the next step the value of b will be the return value from step 1 i.e 3 and so b.x will be undefined...and undefined + anyNumber will be NaN and that is why you are getting that result.
Instead you can try this by giving initial value as zero i.e
arr.reduce(function(a,b){return a + b.x},0);
I used to encounter this is my development, what I do is wrap my solution in a function to make it reusable in my environment, like this:
const sumArrayOfObject =(array, prop)=>array.reduce((sum, n)=>{return sum + n[prop]}, 0)
Just my 2 cents on setting a default value with object literal.
let arr = [{
duration: 1
}, {
duration: 3
}, {
duration: 5
}, {
duration: 6
}];
const out = arr.reduce((a, b) => {
return {
duration: a.duration + b.duration
};
}, {
duration: 0
});
console.log(out);
let temp =[{x:1},
{x:2},
{x:3},
{x:4}];
let sum = temp.map(element => element.x).reduce((a, b) => a+ b , 0)
console.log(sum);
we can used this way for sum of x
Output : 10
reduce function iterates over a collection
arr = [{x:1},{x:2},{x:4}] // is a collection
arr.reduce(function(a,b){return a.x + b.x})
translates to:
arr.reduce(
//for each index in the collection, this callback function is called
function (
a, //a = accumulator ,during each callback , value of accumulator is
passed inside the variable "a"
b, //currentValue , for ex currentValue is {x:1} in 1st callback
currentIndex,
array
) {
return a.x + b.x;
},
accumulator // this is returned at the end of arr.reduce call
//accumulator = returned value i.e return a.x + b.x in each callback.
);
during each index callback, value of variable "accumulator" is
passed into "a" parameter in the callback function. If we don't initialize "accumulator", its value will be undefined. Calling undefined.x would give you error.
To solve this, initialize "accumulator" with value 0 as Casey's answer showed above.
To understand the in-outs of "reduce" function, I would suggest you look at the source code of this function.
Lodash library has reduce function which works exactly same as "reduce" function in ES6.
Here is the link :
reduce source code
to return a sum of all x props:
arr.reduce(
(a,b) => (a.x || a) + b.x
)
You can use reduce method as bellow; If you change the 0(zero) to 1 or other numbers, it will add it to total number. For example, this example gives the total number as 31 however if we change 0 to 1, total number will be 32.
const batteryBatches = [4, 5, 3, 4, 4, 6, 5];
let totalBatteries= batteryBatches.reduce((acc,val) => acc + val ,0)
function aggregateObjectArrayByProperty(arr, propReader, aggregator, initialValue) {
const reducer = (a, c) => {
return aggregator(a, propReader(c));
};
return arr.reduce(reducer, initialValue);
}
const data = [{a: 'A', b: 2}, {a: 'A', b: 2}, {a: 'A', b: 3}];
let sum = aggregateObjectArrayByProperty(data, function(x) { return x.b; }, function(x, y) { return x + y; }, 0);
console.log(`Sum = ${sum}`);
console.log(`Average = ${sum / data.length}`);
let product = aggregateObjectArrayByProperty(data, function(x) { return x.b; }, function(x, y) { return x * y; }, 1);
console.log(`Product = ${product}`);
Just wrote a generic function from previously given solutions. I am a Java developer, so apologies for any mistakes or non-javascript standards :-)
A generic typescript function:
const sum = <T>(array: T[], predicate: (value: T, index: number, array: T[]) => number) => {
return array.reduce((acc, value, index, array) => {
return acc + predicate(value, index, array);
}, 0);
};
Example:
const s = sum(arr, (e) => e.x);
var arr = [{x:1}, {x:2}, {x:3}];
arr.map(function(a) {return a.x;})
.reduce(function(a, b) {return a + b});
console.log(arr);
//I tried using the following code and the result is the data array
//result = [{x:1}, {x:2}, {x:3}];
var arr2 = [{x:1}, {x:2}, {x:3}]
.reduce((total, thing) => total + thing.x, 0);
console.log(arr2);
// and I changed the code to like this and it worked.
// result = 6
We can use array reduce method to create new Object and we can use this option to sum or filter
const FRUITS = ["apple", "orange"]
const fruitBasket = {banana: {qty: 10, kg:3}, apple: {qty: 30, kg:10}, orange: {qty: 1, kg:3}}
const newFruitBasket = FRUITS.reduce((acc, fruit) => ({ ...acc, [fruit]: fruitBasket[fruit]}), {})
console.log(newFruitBasket)
Array reduce function takes three parameters i.e, initialValue(default
it's 0) , accumulator and current value .
By default the value of initialValue will be "0" . which is taken by
accumulator
Let's see this in code .
var arr =[1,2,4] ;
arr.reduce((acc,currVal) => acc + currVal ) ;
// (remember Initialvalue is 0 by default )
//first iteration** : 0 +1 => Now accumulator =1;
//second iteration** : 1 +2 => Now accumulator =3;
//third iteration** : 3 + 4 => Now accumulator = 7;
No more array properties now the loop breaks .
// solution = 7
Now same example with initial Value :
var initialValue = 10;
var arr =[1,2,4] ;
arr.reduce((acc,currVal) => acc + currVal,initialValue ) ;
/
// (remember Initialvalue is 0 by default but now it's 10 )
//first iteration** : 10 +1 => Now accumulator =11;
//second iteration** : 11 +2 => Now accumulator =13;
//third iteration** : 13 + 4 => Now accumulator = 17;
No more array properties now the loop breaks .
//solution=17
Same applies for the object arrays as well(the current stackoverflow question) :
var arr = [{x:1},{x:2},{x:4}]
arr.reduce(function(acc,currVal){return acc + currVal.x})
// destructing {x:1} = currVal;
Now currVal is object which have all the object properties .So now
currVal.x=>1
//first iteration** : 0 +1 => Now accumulator =1;
//second iteration** : 1 +2 => Now accumulator =3;
//third iteration** : 3 + 4 => Now accumulator = 7;
No more array properties now the loop breaks
//solution=7
ONE THING TO BARE IN MIND is InitialValue by default is 0 and can be given anything i mean {},[] and number
//fill creates array with n element
//reduce requires 2 parameter , 3rd parameter as a length
var fibonacci = (n) => Array(n).fill().reduce((a, b, c) => {
return a.concat(c < 2 ? c : a[c - 1] + a[c - 2])
}, [])
console.log(fibonacci(8))
you should not use a.x for accumulator , Instead you can do like this
`arr = [{x:1},{x:2},{x:4}]
arr.reduce(function(a,b){a + b.x},0)`

Categories