+ 4
How to loop of the value of a object key compare it to another value if match than change it to another value
I have nested object call userGraph var userGraph ={ Book:{copyBook:2,brownBook:3}, Shoes:{whiteshoes:3, blackshoes:4} } I want to loop over the value of all the keys of my user graph object if any key matches copyBook then I want to change that key to something else like blackBook so the new Object will look like var userGraph ={ Book:{blackBook:2,brownBook:3}, Shoes:{whiteShoes:3,blackShoes:4} } Thank in advance if you provide a answer
8 Answers
+ 1
Sir.George S Mulbah II This may be what you want.
https://code.sololearn.com/cU6V80V83VYx/?ref=app
+ 2
Sir.George S Mulbah II, huh?
1. I found out that using question ID is better because there may be questions with the same name but different problem. If you meant that I did not add the link as comment, I decide to keep Q&A programs private, and at the moment I used version of Sololearn that does not allow to view comments at private codes from the code view.
2. What do you mean by "appearing to be in replacing the Item but it did not replace it"?
3. You wrote by tags that you want the JavaScript program, so I did it in pure JavaScript... I did not know that you want a node.js program. I would suggest enter "node.js" tag if you want a node.js program.
P. S. I made some changes like generalizing and better explanations.
[edit]
Oh, I see, I did not see that copyBook should be replaced with blackBook. Edited the code again.
+ 1
get please do it in the playground
+ 1
get And if it work pls đ reply me
+ 1
Why Aws Amplify never provided authentication support for React-native typescript template
0
//userGraphCopy = JSON(userGraph) // can prevent some problems (then you need to replace userGraph with userGraphCopy in the r)
for (item in userGraph) {
if (item == targetItemNameString) { // or ===, depending on whether you want to compare the type or no, or item.match(targetItemNameRegExp) to replace items which match the blueprint
let value = userGraph[item];
delete userGraph[item];
userGraph[replaceItemNameString] = value;
}
// use else if or make the in-loop structure "switch" to do it for several cases
}
0
get this your example code has no link with the question I see your code appearing to be in replacing the Item but it did not replace it also I was expecting you to write your solution in node.js but thank any way but this solution is wrong to admit