Returns undefined. How ever if i console.log inside the map function that prints right value.
const myfun = (data) => {
data.map((elem) => {
return elem[1]
});
};
let demo = [[1,2,3],[1,2,3]]
console.log(myfun(demo));
// gives undefined
// should give 2 2
Yes your function returns nothing, therefore undefined.
I think what you meant to write was this.
const myfun = (data) => (
data.map((elem) => {
return elem[1]
})
);
notice how the myfun function is not using curly braces instead it's using regular brackets.
or this.
const myfun = (data) => data.map((elem) => {
return elem[1]
})
If you use curly brackets on an anonymous function you must explicitly use the return keyword.
This happens because you need to return the map, not just what is mapped
const myfun = (data) => {
// You need to return the map
return data.map((elem) => {
return elem[1]
});
};
let demo = [[1,2,3],[1,2,3]]
console.log(myfun(demo));