Access object property in array of objects

前端 未结 3 1886
粉色の甜心
粉色の甜心 2020-12-03 19:35

I have this set

var data = [
    {\"outlet_name\":\"Easy Lane Supermart\",\"20130102_20130108\":\"0\"},
    {\"outlet_name\":\"Eunilaine Foodmart Kalayaan\",         


        
相关标签:
3条回答
  • 2020-12-03 19:43
    $.each(data, function (i, item) {
        console.log(item.outlet_name);
    });
    

    Or without jQuery:

    for (var i=0;i<data.length;i+=1) {
        console.log(data[i].outlet_name);
    }
    

    Ok, if you want to iterate over each object you can write:

    $.each(data, function (i, item) {
        console.log("Values in object " + i + ":");
        $.each(item, function(key, value) {
            console.log(key + " = " + value);
        });
    });
    
    0 讨论(0)
  • 2020-12-03 19:55

    This will provide exact answer...

    var data = [
        {"outlet_name":"Easy Lane Supermart","20130102_20130108":"0"},
        {"outlet_name":"Eunilaine Foodmart Kalayaan","20130102_20130108":"0"},
        {"outlet_name":"PUREGOLD PRICE CLUB, INC - VISAYAS","20130102_20130108":"0"}
    ];
    for(i=0;i<data.length;i++){
    	for(var x in data[i]){
    	    console.log(x + " => " + data[i][x]);
    	}
    }

    0 讨论(0)
  • 2020-12-03 20:03

    If anyone is needing to do this from a JSON string for example

    var myJson = [{"Code":"slide_1.png","Description":"slide_1"},{"Code":"slide_2.png","Description":"slide_2"},{"Code":"slide_3.png","Description":"slide_3"}]
    

    You can use var newJsonArray = JSON.Parse(myJson) and you will get Array[3] 0 : Object 1 : Object 2 : Object

    At which point you can access it by simply saying newJsonArray[i].Code or whatever property inside the array you want to use. Hope this helps!

    0 讨论(0)
提交回复
热议问题