admin管理员组文章数量:1405507
I know this is a simple question but I'm not understanding why my code behaves the way it does. I'm trying to dynamically add a property to an array of objects using array.map(). I can get my code to work the way I want to and make my tests pass but I have to hard code the key which doesn't make the function flexible/reusable and is sort of a 'hack'.
Example:
// this works but I have to hard code the key 'profession' & not quite how I want it to work
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, profession: value }))
}
// I don't understand why this doesn't work...
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, obj[key]: value }))
}
// or this doesn't work either...
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, obj['key']: value }))
}
// or this...even if I'm already enclosing the key in template strings
// which should effectively set the key as a string which is the reason
// why my first example worked
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, `${key}`: value }))
}
addKeyValue([{name: 'Moe'}, {name: 'Larry'}, {name: 'Curly'}], 'profession', 'edian')
// [{name: 'Moe', profession: 'edian'}, {name: 'Larry', profession: 'edian'}, {name: 'Curly', profession: 'edian'}]
I know it's probably a really simple thing I'm overlooking and also not understanding so thanks in advance for everybody's help! : )
I know this is a simple question but I'm not understanding why my code behaves the way it does. I'm trying to dynamically add a property to an array of objects using array.map(). I can get my code to work the way I want to and make my tests pass but I have to hard code the key which doesn't make the function flexible/reusable and is sort of a 'hack'.
Example:
// this works but I have to hard code the key 'profession' & not quite how I want it to work
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, profession: value }))
}
// I don't understand why this doesn't work...
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, obj[key]: value }))
}
// or this doesn't work either...
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, obj['key']: value }))
}
// or this...even if I'm already enclosing the key in template strings
// which should effectively set the key as a string which is the reason
// why my first example worked
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, `${key}`: value }))
}
addKeyValue([{name: 'Moe'}, {name: 'Larry'}, {name: 'Curly'}], 'profession', 'edian')
// [{name: 'Moe', profession: 'edian'}, {name: 'Larry', profession: 'edian'}, {name: 'Curly', profession: 'edian'}]
I know it's probably a really simple thing I'm overlooking and also not understanding so thanks in advance for everybody's help! : )
Share Improve this question asked Jan 22, 2020 at 20:04 tedicotedico 675 bronze badges 1- 1 Does this answer your question? Add a property to a JavaScript object using a variable as the name? – 0bero Commented Jan 22, 2020 at 20:09
2 Answers
Reset to default 4In order to use an expression as the key in an object literal, put it in []
.
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, [key]: value }))
}
See Create an object with dynamic property names
You need a puted property name.
function addKeyValue(arr,key,value) {
return arr.map((obj) => ({...obj, [key]: value }));
}
console.log(addKeyValue([{ name: 'Moe' }, { name: 'Larry' }, { name: 'Curly' }], 'profession', 'edian'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文标签: javascriptHow can I dynamically add a key value pair to an array of objects using mapStack Overflow
版权声明:本文标题:javascript - How can I dynamically add a key value pair to an array of objects using map - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744920891a2632285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论