javascript - Returning an object after reduce function - Stack Overflow

Im trying to use reduce in bination with the spread operator to practice. I have an array that i wish

Im trying to use reduce in bination with the spread operator to practice. I have an array that i wish to convert into an object. I tried doing this, but my reduce function only returns the last value of the array. I wish this reducer function to add on to the array.

My try :

const arr = ['1' , '4' , '3' , '5' , '3'];

let obj;
arr.reduce((acc ,val) => {
    obj = {...acc , [val]:val};
},{});

console.log(obj)

Why do i only get the last value? and not the entire array to an object?

Im trying to use reduce in bination with the spread operator to practice. I have an array that i wish to convert into an object. I tried doing this, but my reduce function only returns the last value of the array. I wish this reducer function to add on to the array.

My try :

const arr = ['1' , '4' , '3' , '5' , '3'];

let obj;
arr.reduce((acc ,val) => {
    obj = {...acc , [val]:val};
},{});

console.log(obj)

Why do i only get the last value? and not the entire array to an object?

Share Improve this question edited Aug 5, 2019 at 14:58 Kevin.a asked Aug 5, 2019 at 14:54 Kevin.aKevin.a 4,32615 gold badges51 silver badges92 bronze badges 1
  • 1 Why are you assigning inside a reduce? arr.reduce((acc ,val) => ({...acc, [val]: val}), {}); returns the new object you want. – jonrsharpe Commented Aug 5, 2019 at 14:57
Add a ment  | 

3 Answers 3

Reset to default 4

The reduce callback expects a return if expressed like you did. In your scenario, nothing is returned from your reduce callback, because obj is assigned but never returned, hence acc in the second iteration is not defined and obj is assigned in each cycle, resulting in the last element of arr spread to an object.

If you want to do that in a single line, you can also avoid assigning to obj, by just using reduce as intended, by spreading progressively to a brand new object. The value returned by reduce will be assigned to obj.

const arr = ['1' , '4' , '3' , '5' , '3'];

let obj = arr.reduce((acc, val) => ({...acc, [val]: val}), {});
//                                                         ^--- initial value
console.log(obj);

That's because you did not return the balue from reduce

const arr = ['1' , '4' , '3' , '5' , '3'];

let obj = arr.reduce((acc ,val) => {
    const obj = {...acc , [val]:val};
    return obj;
},{});

console.log(obj)

or simplified

const arr = ['1', '4', '3', '5', '3'];
const obj = arr.reduce((acc, val) => ({ ...acc,[val]: val}), {});
console.log(obj)

You need to return the new value of the accumulator out of the reducing function

const arr = ['1' , '4' , '3' , '5' , '3'];

const obj = arr.reduce((acc ,val) => {
    return {...acc , [val]:val};
},{});

console.log(obj);

You can shorten that a bit further by returning the object literal wrapped in parentheses. See returning object literals

const arr = ['1' , '4' , '3' , '5' , '3'];

const obj = arr.reduce((acc ,val) => ({...acc , [val]:val}), {});

console.log(obj);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745551968a4632617.html

相关推荐

  • javascript - Returning an object after reduce function - Stack Overflow

    Im trying to use reduce in bination with the spread operator to practice. I have an array that i wish

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信