Shilpa
Published in : 2022-03-05
I am frustrated by using the below approach in javascript. what could be the possible solution to not create a new object and delete the key?
var rowObj = { name : 'shilpa', id: 1};rowObj.name_new= rowObj.name;rowObj.id_new = rowObj.id;delete rowObj.name;delete rowObj.id;alert(JSON.stringify(rowObj))
An optimized approach for this issue will be highly appreciated.
Join our community and get the chance to solve your code issues & share your opinion with us
Sign up Now
Rakshit Date : 2022-03-05
Best answers
34
Best answers
34
Definitely, you are not creating another object from ‘rowObj’ using your code.
Inside object, if you want to add suffix or prefix to all the keys as ‘_new’ or ‘new_’, you need to loop your all the keys.
Try below code,
Hope, it will work for you!
Shilpa Date : 2022-03-05
Best answers
10
Best answers
10
Yeah, this can be done in the best way, thanks for sharing.