odedta
odedta

Reputation: 2478

How to get items from an array of objects

I used jQuery.parseJSON to parse a JSON of objects that looks like this (Chrome Developers Console):

[Object, Object, Object, Object]
0: Object
    feedback_q1: "lol"
    subtotal: "13"
    __proto__: Object
1: Object
    feedback_q1: "lal"
    subtotal: "3"
    __proto__: Object
2: Object
3: Object
4: Object

How can I pick, using javascript or jQuery, each object (not using loops) and its properties?

Upvotes: 1

Views: 122

Answers (2)

DDan
DDan

Reputation: 8276

If you know that you want to get prop1 value from Xth element than json_objects[X].prop1. Is there a real-life scenario when you want to do this?

Maybe you want ed to get the element where...

If you want to get element where prop1 value is 'lol' then you can use JQuery grep

$.grep(json_objects, function(o){ return o.prop1 == 'lol'; });

The result of $.grep is an array with the items found which match your criteria.

Upvotes: 2

tomiy
tomiy

Reputation: 156

I think

JSONObject[0]

should retrieve the 1st object in your JSON object. You can then get its properties by just doing

JSONObject[0].subtotal

Upvotes: 2

Related Questions