I am working on a project; the object has a dynamic key and a dynamic value. I will find which number the keys are holding, and change the number to string value, assuming the object list is too big to check each individually- more than 30 key values
const obj = {
one: 'jkej',
two: 123,
three: 'abc'
}
Required output is
const obj = {
one: 'jkej',
two: '123',
three: 'abc'
}
I want to change the what are the keys holding, and change the number value to string.
You can loop the keys of the object and convert the values to string
Object.keys(obj).forEach((k) => {
obj[k] = obj[k].toString()
})
You can do something like this
const obj = {
one: 'jkej',
two: 123,
three: 'abc'
}
const convertToString = (object) =>
Object.fromEntries(
Object.entries(object)
.map(([k, v]) => [k, v + ''])
)
console.log(convertToString(obj))
You can iterate through the object and cast everything into a string using toString():
for (let [key, value] of Object.entries(myObject)) {
value = value.toString()
}