Reputation: 20368
Given an object
const firstObject = {
key1 : 'value1',
key2 : 'value2'
// Many more keys here
};
I want to create another object like this
const secondObject = {
key1 : false,
key2 : false
// Many more keys here same as firstObject
};
I know that I can just do Object.keys()
and iterate over them to add the values, or do a shallow copy and then change the values, but I am looking for a performant solution.
So what will be the most performant way to achieve it, given that the keys are dynamic (not hardcoded anywhere) and we just need the solution for one level deep keys only?
PS: ES6 solutions are fine as well, I am using babel anyway.
Upvotes: 0
Views: 56
Reputation: 138557
Object.fromEntries( Object.keys(firstObject).map(k => ([k, false])))
Wether that is "more performant" has to be tested.
Upvotes: 1