【发布时间】:2022-12-01 14:12:09
【问题描述】:
Need to flat or remove platform key from nested object like this, platform key will be same for every object
climb up nested object properties to one level up and remove key value(platform) to make data consistent, as platform key is different for nested objects
{
option1: {
platform : {
key1:value1,
key2:value2,
key3:value3
}
},
option2: {
platform : {
key1:value1,
key2:value2,
key3:value3
}
},
option3: {
platform : {
key1:value1,
key2:value2,
key3:value3
}
}
}
Expected output is like
{
option1: {
key1:value1,
key2:value2,
key3:value3
},
option2: {
key1:value1,
key2:value2,
key3:value3
},
option3: {
key1:value1,
key2:value2,
key3:value3
}
}
I tried approach using for loop and if condition, but that seems expensive operations. I need a more simpler way to do that.
【问题讨论】:
-
Please add the code you've tried. Have you tried looping through the keys of the object? A simple loop like
for(const key in data) data[key] = data[key].platformwill work.
标签: javascript json object