This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed last year.
Why does the second assignment need return and the first returns variable total automatically?
const total = inventors.reduce(((total, inventor) => total += inventor.passed - inventor.born), 0);
const transportation = data.reduce(((prevVal, currentVal) => {
if (!prevVal[currentVal]) {
prevVal[currentVal] = 0;
}
prevVal[currentVal]++;
return prevVal;
}), {})
If return prevVal is omitted I get Uncaught TypeError: Cannot read properties of undefined
What is the difference in how reduce() acts with values. Is it effected by ternary operation? If so then why this returns 0?
const transportation = data.reduce(((prevVal, currentVal) =>
(!prevVal[currentVal]) ? prevVal[currentVal] = 0 : prevVal[currentVal]++), {})
When using curly braces with an arrow function, the return is not implicit, so you have to use return
Related
This question already has answers here:
Can `Array.find` use a default value if the callback function doesn't return anything truthy?
(2 answers)
Javascript: default value on missing property
(2 answers)
Closed 15 days ago.
module.exports.get = function (data) {
const _data = require("./data/Users.json");
if (_data.find(a => a.uid === data)) {
return a.name
} else return "User"
}
I want to return a.name but it says that a is not defined
The find method of array returns the first element that matches the provided condition; if no element is found, undefined is then returned. Thus:
const a = _data.find(user => user.uid === data)
if (a) {
return a.name
} else {
return "User"
}
It is recommended to give arguments and variables good names for better readability and maintainability. The following code can do the same thing, but is usually considered easier to understand:
module.exports.get = function (uid) {
const users = require("./data/Users.json");
const user = users.find(user => user.uid === uid);
return user?.name ?? "User";
}
The single question mark and double question marks are optional chaining operator and nullish coalescing operator respectively, which is not a must. Using if/else is perfectly fine in this case.
This question already has answers here:
Loop until... with Ramda
(2 answers)
Closed 3 years ago.
I want a function that returns a value when a function returns a value which is not null.
const getSomeValue = R.filter(...);
const getSomeOtherValue = R.propEq('name');
The R.until function is probably the one to use.
Some pseudo code:
R.until(R.isNotNull, R...[getSomeValue, getSomeOtherValue]);
So run through the functions until the returned value is not null.
I don't find any suitable function in the Ramda docs to do this.
Any ideas?
If you don't mind to also ignore falsy values (false, undefined, 0, etc...) then you could use R.either:
const first = () => {
console.log("first");
return null;
};
const second = () => {
console.log("second");
return {a: "banana"};
};
const third = () => {
console.log("third");
return {a: "chicken"};
};
const fn = R.either(first, second, third);
console.log(fn());
<script src="https://unpkg.com/ramda#0.26.1/dist/ramda.min.js"></script>
This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
Its known that someone can make a one-line array function like this to return the single value:
var func = (x) => x + 1 //func(2) == 3
and you can also make multi-line array-functions, whose values need to be manually returned:
var funcMultiline = (x) => {
var result = 1;
result += x;
return result;
}
funcMultiline(4) == 5; //true
So the question:
let's say I want to return a new object in one line, if I use the brackets, then the array-function is treated like a multi-line function, and doesn't actually return the object-literal. Is there any direct way to create an object literal in, lets say, a map function? Like:
[...Array(25)].map(e => {x: 5, y:10}) //this is a syntax error, but how can I make this work
Returning object literals using the concise body syntax params => {object:literal} will not work as expected.
You have to wrap the object literal with parenthesis:
var res = [...Array(25)].map(e => ({x: 5, y:10}))
console.log(res);
This question already has an answer here:
Why doesn't my arrow function return a value?
(1 answer)
Closed 4 years ago.
So to my understanding which is obviously wrong at this moment in time is that,
return arg => arg*2
is the same as
return (arg)=>{arg*2}
I always assumed arrow functions are just syntactically neater.
But doing this with closures like so doesn't work.
function addTwoDigits(firstDigit){
return (secondDigit)=>{firstDigit + secondDigit}
}
let closure = addTwoDigits(5);
console.log(closure(5)) // Undefined
Yet this is fine
function addTwoDigitsV2(firstDigit){
return secondDigit => firstDigit + secondDigit
}
let closure2 = addTwoDigitsV2(10);
console.log(closure2(10))// 20
arrow function works differently here:-
(x)=> x*2 ; // dont have to return anything, x*2 will be returned
is not same as
(x) =>{x*2}
//here you need to return something otherwise undefined will be returned
When you use {} you must set return
return (arg)=>{return arg*2}
The arrow function only supplies the return automatically if you have an expression after the arrow. If the arrow is followed by a curly brace, it's treated as the braces around a function body, so you have to write return explicitly, i.e.
arg => arg * 2
is equivalent to:
(arg) => { return arg * 2; }
function addTwoDigits(firstDigit) {
return (secondDigit) => {
return firstDigit + secondDigit
}
}
let closure = addTwoDigits(5);
console.log(closure(5))
You need a return statement if the body of your arrow function is wrapped in { ... }.
Since yours is a single expression, you can skip the {-} and return. But if you have the {-}, you need the return statement.
The parentheses are not an issue here. You need them if you have more than a single parameter. With one, they're optional.
This question already has answers here:
In Javascript, when is it necessary to assign a named function to a variable?
(3 answers)
Closed 6 years ago.
I'm learning some more ES6:
const _findCartItem = ( item ) => {
return _cartItems.find( cartItem => cartItem.id === item.id);
};
converts to:
var _findCartItem = function _findCartItem(item){
return _cartItems.find(function(cartItem){
return cartItem.id == item.id;
})
};
However, is there a difference between this and the following? Is this dual declaration necessary?
var _findCartItem = function(item){
return _cartItems.find(function(cartItem){
return cartItem.id == item.id;
})
};
Conceitualy, no, both will do the same. Syntactically, yes. First one is passing a reference of a named function to a variable, second a anonymous function.
Using first option is prefereble because you get better call stacks.