Adding {} to map breaks it [duplicate] - javascript

This question already has answers here:
Why doesn't my arrow function return a value?
(1 answer)
When should I use a return statement in ES6 arrow functions
(6 answers)
Arrow function without curly braces
(9 answers)
Curly Brackets in Arrow Functions
(3 answers)
Closed 1 year ago.
Why does this work:
const final = pdata.map((p) => p.nodeName);
// returns [ 'H1', 'P', 'P' ] like its supposed to
But this returns undefined in all of them:
const final = pdata.map((p) => {
p.nodeName
});
// returns [ undefined, undefined, undefined ]
I need to add a couple if statements inside to check for the different types but the {} seems to break it. Am I not supposed to this this in a .map()? Or is there another way to do this?

The usage of {...} is to encapsulate multiple statements.
You need to specify the return keyword:
const final = pdata.map((p) => {
return p.nodeName;
});

Related

Why does every() method work only one line? [duplicate]

This question already has answers here:
Why doesn't my arrow function return a value?
(1 answer)
Curly Brackets in Arrow Functions
(3 answers)
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed 1 year ago.
The following code works:
let check = checkingImages.every((item)=>item.classList.contains('matched'))
But this one doesn't:
let check = checkingImages.every((item)=>{
item.classList.contains('matched')
})
Can someone explain what's the reason for the second code not working, please?

... Spread operator with already an array as parameter [duplicate]

This question already has answers here:
Spread Syntax vs Rest Parameter in ES2015 / ES6
(11 answers)
What is the meaning of "foo(...arg)" (three dots in a function call)?
(4 answers)
Closed 1 year ago.
I have this working code (puppeteer) :

async function extractedEvaluateCall(page) {
await page.waitForXPath(xpath);
const links = await page.$x(xpath);
const results = await page.evaluate((...links) => {
return links.map(e => e.innerText);
}, ...links);
}
What does the notation ... does here ?
I tried without, it doesn't work:
UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON Are you passing a nested JSHandle?
links is already an array, why the need of spread operator ?

Javascript map() method and arrow function [duplicate]

This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
How is () => {...} different from () => [duplicate]
(4 answers)
Arrow function without curly braces
(9 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 2 years ago.
This probably suits other methods as well but the one i'm using at the moment is map().
How come:
const singleFruit = fruits.map((fruit) => fruit.toUpperCase());
console.log(singleFruit);
returns the array the correct way, with everything in uppercase, when:
const singleFruit = fruits.map((fruit) => {
fruit.toUpperCase();
});
console.log(singleFruit);
gives me an array but with my assigned fruits are now undefined. I can solve this by adding return before "fruit.toUpperCase();" I thought that the second code was the exact same as the first one but without the curly braces.
Thanks!

Strange syntax error with map and arrow functions [duplicate]

This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 6 years ago.
Never seen this one before:
.flatMap(obj => {
return removeOneLine(this)
.map(l => {l:l,id:obj.id});
I want to map the result, but of course, I guess JS doesn't know if this is an object or the function body.
Is doing the following the only way to avoid a syntax error (because it's ambiguous to the engine):
.flatMap(obj => {
return removeOneLine(this)
.map(l => {
return {l: l, id: obj.id}
});
})
is this ambiguity in this situation normal, any way to mitigate besides what I just did above?
Why do you have the curlies for a single-statement fat-arrow function?
You don't need curly braces around the function bodies or an explicit return statement. You do, however, need to put parentheses around the object literal to prevent its being interpreted as a function body.
.flatMap( obj=> removeOneLine(this).map(l => ({ l: l, id: obj.id })) )

Converting javascript array of objects to array of other objects using map method [duplicate]

This question already has an answer here:
nodejs arrow function with expression [duplicate]
(1 answer)
Closed 6 years ago.
I want to convert one array to another kind of array.
Actually I am using typescript.
What am I doing wrong here:
//terminals is an array of objects.
groupOptions = terminals.map(trm => {
id: trm.TerminalID,
text: trm.TerminalName,
selected: true
});
intelisence complains about the body of the curly brackets. With them I meant an object, probably intellisence thinks its anonymous method body. How can I workaround this?
Try wrapping the curly brackets with parenthesis like this:
//terminals is an array of objects.
groupOptions = terminals.map(trm => ({
id: trm.TerminalID,
text: trm.TerminalName,
selected: true
}) );
Problem is that the JavaScript runtime picks up the curly brackets as beginning/end of a function.

Categories