arrays - Return a new object with some extra properties in JavaScript - Stack Overflow

I have an objectsusersById: {1: { name: 'John' },2: { name: 'Michelle' },...}I wan

I have an objects

usersById: {
  1: { name: 'John' },
  2: { name: 'Michelle' },
  ...
}

I want to return the same object, but first populate the object at id=2 with a new property age, but sticking to immutability.

I would guess it would be something like

return {
  ...usersById,
  ...usersById[2].age = 40
}

but I receive an error In this environment the sources for assign MUST be an object. This error is a performance optimization and not spec pliant.

Alternatively, I would guess it should be something like

return Object.keys(usersById).map(userId => {
  if (userId === 2) {
    return {
      ...usersById[2],
      ...age = 40
    }
  }
  return usersById[userId]
})

but it returns an array and not an object.

I have an objects

usersById: {
  1: { name: 'John' },
  2: { name: 'Michelle' },
  ...
}

I want to return the same object, but first populate the object at id=2 with a new property age, but sticking to immutability.

I would guess it would be something like

return {
  ...usersById,
  ...usersById[2].age = 40
}

but I receive an error In this environment the sources for assign MUST be an object. This error is a performance optimization and not spec pliant.

Alternatively, I would guess it should be something like

return Object.keys(usersById).map(userId => {
  if (userId === 2) {
    return {
      ...usersById[2],
      ...age = 40
    }
  }
  return usersById[userId]
})

but it returns an array and not an object.

Share Improve this question asked Oct 19, 2017 at 16:00 JamgreenJamgreen 11.1k32 gold badges122 silver badges232 bronze badges 3
  • 1 Map always returns an array, as it's an array method. – evolutionxbox Commented Oct 19, 2017 at 16:01
  • I know, but isn't reduce also an array method that could return an object with arr.reduce((accumulator, item) => { ... }, {})? – Jamgreen Commented Oct 19, 2017 at 16:05
  • It's not a tautology. Array methods do not always return an array. I shouldn't have used that as an explanation. – evolutionxbox Commented Oct 19, 2017 at 16:28
Add a ment  | 

2 Answers 2

Reset to default 7

You've got the right idea but the wrong syntax. Try this instead:

return {
  ...usersById,
  2: {
    ...usersById[2],
    age: 40
  }
}

Or if the key is dynamic, you can do this:

let key = 2;
return {
  ...usersById,
  [key]: {
    ...usersById[key],
    age: 40
  }
}

You can make your own function to return same object with populated values

Simple example:

var usersById =  {
  1: { name: 'John' },
  2: { name: 'Michelle' },
}

usersById = oneLevelDeepAssign(usersById,2,{age:21})


function oneLevelDeepAssign(object, key, objectToAssign){

  return Object.assign({},object,{[key]:Object.assign({},object[key],objectToAssign)})

}

console.log(usersById);

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信