Json Object Value Based On Other Object Value
I need to find a value based on another object value json = {[{ID:'1',city:'Atlanta'},{ID:'2',city:'New York'}]} and so forth. I need to find the value of a city where ID is x. Is
Solution 1:
You could format it as follows
vardata = {
id: "city",
1: "Atlanta",
2: "New York",
6: "New Jersy",
24: "San Diego"
};
At which point, access can be done using the ID and the array access operator
console.log(data[2], data[24]);
yields
New York San Diego
Solution 2:
You could consider using JSONPath, JSONQuery, jLinq, etc... although under-the-hood there's a very good chance they will use loops.
Solution 3:
Why don't you store this like an array
array = ["Atlanta", "New York"];
Calling array[0]
will return "Atlanta"
.
If you have to use json you will need to use loops to do what you want.
Post a Comment for "Json Object Value Based On Other Object Value"