I´m trying to set some values within a object using forEach lambda:
var row = {title: "", attribute:"", width: ""};
list.forEach( list =>
row.title = list.label |
row.attribute = list.label |
row.width = "300px"
);
Works fine only with a statement row.title = list.label
when I add the rest of parameters does not work fine.
What is the right syntax?
3) JavaScript Lambda Function Can Duplicate Codelet global = "something" let foo = (input) => { global = "somethingElse" } let bar = () => { if (global === "something") { //... } }
The syntax of a lambda function is lambda args: expression . You first write the word lambda , then a single space, then a comma separated list of all the arguments, followed by a colon, and then the expression that is the body of the function.
A lambda expression is the code you type to define a short function. It is source code text that goes into the compiler and is recognized with a particular syntax. (In Javascript, technically they are called arrow function expressions/declarations.) The expression evaluates at run time to a lambda function in memory.
It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.
Try:
var row = {title: "", attribute:"", width: ""};
list.forEach( list => {
row.title = list.label;
row.attribute = list.label;
row.width = "300px"
});
Notice the curly braces.
You need curly brackets, as the part after the =>
is a function body:
var row = {title: "", attribute:"", width: ""};
list.forEach( list => {
row.title = list.label;
row.attribute = list.label;
row.width = "300px";
});
(Be advised that if this is the code you are actually running, the values in row
will be set to the values of the last entry in list.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With