I want to update my object values with some predefined value like 5.
Here is my object,
let data = {"a":1,"b":2,"c":3,"d":5}
Expected Output;
let data = {"a":5,"b":10,"c":15,"d":25}
Here is my attempt:
Object.keys(data).map(key => data[key] *= 5 )
Just loop through all the new object's key and update it to existing object.
var obj = {"a":1,"b":2,"c":3,"d":5,"f":9,"g":11,"h":21}
var newObj = {"a":5,"b":10,"d":20,"h":25}
let keys = Object.keys(newObj)
keys.map(x=>{
obj[x] = newObj[x]
})
console.log(obj)
Multiply that values with 5:
let data = {"a":1,"b":2,"c":3,"d":5};
Object.keys(data).forEach(key => data[key] *= 5);
console.log(data);
Using Object.Entries()
let data = {"a":1,"b":2,"c":3,"d":5};
Object.entries(data).forEach(([key, val]) => data[key] = val*5);
console.log(data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With