2

Selecting the JSON element

 2 years ago
source link: https://www.codesd.com/item/selecting-the-json-element.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Selecting the JSON element

advertisements

my JSON objects look like this:

[{"aid":"1","atitle":"Ameya R. Kadam"},{"aid":"2","atitle":"Amritpal Singh"},{"aid":"3","atitle":"Anwar Syed"},{"aid":"4","atitle":"Aratrika"},{"aid":"5","atitle":"Bharti Nagpal"}]

As you can see the names are differentiated through their associated aid's. Now suppose I want to display the name stacked with aid: 4. what js should i write for that?


You could loop over the elements of your array, testing, for each one, if its aid is 4 :

var list = [{"aid":"1","atitle":"Ameya R. Kadam"},
        {"aid":"2","atitle":"Amritpal Singh"},
        {"aid":"3","atitle":"Anwar Syed"},
        {"aid":"4","atitle":"Aratrika"},
        {"aid":"5","atitle":"Bharti Nagpal"}
    ];
var length = list.length;
var i;
for (i=0 ; i<length ; i++) {
    if (list[i].aid == 4) {
        alert(list[i].atitle);
        break; // Once the element is found, no need to keep looping
    }
}

Will give an alert with "Aratrika"


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK