I have an object structured like this:
const obj = {a: {x: 0, y: 0}}
but it can also be structured like this:
const obj = {a: {x: 0, y: 0}, b: {x: 10, y: 3}, abcd: {x: -1, y: 0}}
This means that the obj
can contain multiple keys with variable key names.
I want to replace each x
value with a string in the format ${x}%
, where x
is the original value followed by a percentage symbol.
How can I achieve this transformation?
The expected output should look like:
const obj = {a: {x: 0, y: 0}} // {a: {x: '0%', y: 0}}
const obj = {a: {x: 0, y: 0}, b: {x: 10, y: 3}, abcd: {x: -1, y: 0}} // {a: {x: '0%', y: 0}, b: {x: '10%', y: 3}, abcd: {x: '-1%', y: 0}}
I attempted looping through the object but I'm unsure if there is a more efficient solution available.