How to loop of the value of a object key compare it to another value if match than change it to another value | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

12th Apr 2021, 3:24 PM
George S Mulbah II
George S Mulbah II - avatar
8 Answers
12th Apr 2021, 11:54 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 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.
13th Apr 2021, 11:36 AM
#0009e7 [get]
#0009e7 [get] - avatar
+ 1
get please do it in the playground
12th Apr 2021, 4:16 PM
George S Mulbah II
George S Mulbah II - avatar
+ 1
get And if it work pls 🙏 reply me
12th Apr 2021, 4:17 PM
George S Mulbah II
George S Mulbah II - avatar
+ 1
Why Aws Amplify never provided authentication support for React-native typescript template
30th Jun 2021, 2:12 PM
George S Mulbah II
George S Mulbah II - avatar
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 }
12th Apr 2021, 3:50 PM
#0009e7 [get]
#0009e7 [get] - avatar
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
12th Apr 2021, 8:26 PM
George S Mulbah II
George S Mulbah II - avatar