E
E
exxagw2014-12-11 19:54:40
JavaScript
exxagw, 2014-12-11 19:54:40

How to get json field by key?

var json =  [
    {"id":"88","pagetitle":"test 88","longtitle":"","description":"описание","alias":"test/id88"},
    {"id":"99","pagetitle":"test 99","longtitle":"","description":"описание","alias":"test/id99"},
    {"id":"150","pagetitle":"test 150","longtitle":"","description":"описание","alias":"test/id150"},
    {"id":"350","pagetitle":"test 350","longtitle":"","description":"описание","alias":"test/id350"}
  ]


How to get the alias value of id = 88?

something in the spirit of

alert(json.id[88].alias);

ps by key (0,1,2,3) should not be obtained ....

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yago, 2014-12-11
@exxagw

By key through the loop

$.each(json, function(i, object) {
    if (object.id == 88) {
        alert(object.alias);
        return;
    }
});

K
Kirill, 2014-12-11
@kirill89

var result = $.grep(json, function(obj){ return obj.id == 88; });

K
keslo, 2014-12-11
@keslo

I'm far from a pro, but I'll try my own version :-)

for (var i = 0; i < json.length; i++) {
    if( json[i].id == 88 ) alert ( json[i].alias );  // вместо вывода в alert() можно присвоить значение в переменную 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question