简体   繁体   中英

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?

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

If you know that you want to get prop1 value from X th 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM