JSON object value based on another object value

I need to find a value based on another value of an object

json = {[{ID:"1",city:"Atlanta"},{ID:"2",city:"New York"}]} 

etc.

I need to find the value of the city where ID is x. Is there a way to do this without using loops?

More: I have to create a json object looping through the document, then send this json to the web service, which returns me another set of json to fill the fields.

0
source share
3 answers

You might consider using JSONPath, JSONQuery, jLinq, etc ... although there is a very good chance that they will use loops under the hood.

+1
source

You can format it as follows

 var data = { id: "city", 1: "Atlanta", 2: "New York", 6: "New Jersy", 24: "San Diego" }; 

At this point, access can be done using the identifier and the array access operator

 console.log(data[2], data[24]); 

gives

New York San Diego

+3
source

Why don't you save this as an array

 array = ["Atlanta", "New York"]; 

A call to array[0] will return "Atlanta" .

If you need to use json, you will need to use loops to do what you want.

0
source

Source: https://habr.com/ru/post/905101/


All Articles