I tried using concat
but unable to get array like [1,2,66]
. By using push
I got. But using concat is it possible or what is the reason I am not getting the result.
const arr = [1, 2, 66];
const data = arr.reduce((obj, k) => {
obj.concat(k);
return obj
},[]);
console.log(data);
The concat() method is used to merge two or more arrays . This method does not change the existing arrays, but instead returns a new array .
So, obj.concat(k)
doesn't do anything, and you're returning an unchanged obj
in the next line.
To solve this you can assign the concat to a new variable and return that...
const newObj = obj.concat(k);
return newObj;
... or simply return the result of the concat:
return obj.concat(k);
const arr = [1, 2, 66]; const data = arr.reduce((obj, k) => { return obj.concat(k); }, []); console.log(data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.