You need to sign in to do that
Don't have an account?

Lightning Components Basics - Input Data Using Forms - Reduce Function
Midway through this module the following Client Controller code is introduced. Is the Reduce function documented anywhere else so I can understand it better. The Trailhead explanation doesn't work for me, very stuck
({
clickCreate: function(component, event, helper) {
var validExpense = component.find('expenseform').reduce(function (validSoFar, inputCmp) {
// Displays error messages for invalid fields
inputCmp.showHelpMessageIfInvalid();
return validSoFar && inputCmp.get('v.validity').valid;
}, true);
// If we pass error checking, do some real work
if(validExpense){
// Create the new expense
var newExpense = component.get("v.newExpense");
console.log("Create expense: " + JSON.stringify(newExpense));
helper.createExpense(component, newExpense);
}
}
})
({
clickCreate: function(component, event, helper) {
var validExpense = component.find('expenseform').reduce(function (validSoFar, inputCmp) {
// Displays error messages for invalid fields
inputCmp.showHelpMessageIfInvalid();
return validSoFar && inputCmp.get('v.validity').valid;
}, true);
// If we pass error checking, do some real work
if(validExpense){
// Create the new expense
var newExpense = component.get("v.newExpense");
console.log("Create expense: " + JSON.stringify(newExpense));
helper.createExpense(component, newExpense);
}
}
})
syntax : array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
for better understanding please refer below link :
https://www.w3schools.com/jsref/jsref_reduce.asp
Let us know if it helps you, and kindly mark it best answer it this answer helps you so it make proper solution for others in future
thanks
All Answers
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.
Read here for more details: reduce function is part of array prototype javascript object. It takes a function as input and applies that on the array and returns the output.
In this case the function does a summation of array members and returns the output.This is achieved by using reduce function which applies it on each and every member.
Hope this helps.
Please like the answer and mark it as best if it helps.
Thanks,
Aman
I was looking for the officail Salesforce documentation.
Specific issue is validSoFar and inputCmp variables are used but never defined anywhere. In your example where are the input variables defined sum, value? That is the bit I don't get, what loads these variables with information for Reduce to do it's thing?
syntax : array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
for better understanding please refer below link :
https://www.w3schools.com/jsref/jsref_reduce.asp
Let us know if it helps you, and kindly mark it best answer it this answer helps you so it make proper solution for others in future
thanks
It is a method that can be called by any JS object (aka variable) (aka value), but only is useful and sensible to use when the JS object, the caller, contains more than one value. In the Trailhead exercise, it is called by a list of lightning:inputField components, which is what component.find(...) returned.
reduce() takes an anonymous function - i will call it "myFunc()" - as the first argument. reduce() takes an initial value - "myInit" - as a second argument.
myFunc() takes two arguments, which i will call myValid and oneValue. reduce() proceeds in this way:
- It sets myValid to myInit (in the Trailhead exercise, this is the final "true").
- Then, for each of the values in the caller, it sets oneValue to that value, and calls myFunc(myValid, oneValue).
- Then it returns whatever the final call to myFunc() returned.
It only is sensible to do this if myFunc() modifies myValid, so that results can acumulate during the iteration. myFunc could add up a list, and return the sum (as above). Or, as in the Trailhead exercise, it can check whether all the field-input-values in the list are all valid, and return that Boolean value. HTH!Lightning Components seem to go against the No Code approach and make Salesforce a very tough environment to build solutions quickly now