简体   繁体   中英

Access to element in javascript Object

After

var parsed = JSON.parse(data);
        var arr = [];
        for(var x in parsed){
          arr.push(parsed[x]);
        }

I have:

arr = [{"text":"sometext"}]
[{"text":"sometextOne"},{"text":"sometextTwo"}]
[{"text":"sometextOther"},{"text":"sometext"}]
[{"text":"sometexts"}]
[{"text":"sometextThree"},{"text":"sometextOne"},{"text":"sometextTwo"}]

What the best way show 'sometext' ?

Example: console.log(array.text) // sometext

array[0].text its a way to show sometext.

If your json array should be like this

var arr=[ {"text":"sometext"}, {"text":"sometext"}, {"text":"sometext"} ]

U can use this code to print sometext

for (var i=0; i<arr.length; i++) {console.log(arr[i].text);}

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