I have this array of object
const d = [{
a_1: 1,
b_2: 2
}]
How can I update a_1
value to 2
without creating a temporary variable?
const myKey = 'a_1'
const myValue = 2
d.map(obj => ({...obj, obj[myKey]:myValue})) //why this won't work?
I have this array of object
const d = [{
a_1: 1,
b_2: 2
}]
How can I update a_1
value to 2
without creating a temporary variable?
const myKey = 'a_1'
const myValue = 2
d.map(obj => ({...obj, obj[myKey]:myValue})) //why this won't work?
Remove the obj
from obj[myKey]
so that [myKey]
is correctly seen as a computed property name.
const transformedDs = d.map(obj => ({...obj, [myKey]:myValue}))