Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access object property in array of objects

I have this set

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"}
];

$.each(data, function (i, item) {
    $.each(item, function (k,v) {
        $('#result').append(k,v);
    });
});

How can I make it only view all the values of outlet_name without using the item.outlet_name?

like image 292
A Super Awesome Avatar asked Oct 03 '13 08:10

A Super Awesome


People also ask

How do you access a property inside an object inside an array?

You can acces items in arrays at given position by their index. In javascript indexes of arrays are starting with 0: myArray[0] . To access the property of the returned object just use dot-notation: myArray[0].

How do you take an object from an array of objects?

Use array. forEach() method to traverse every object of the array. For each object use delete obj. property to delete the certain object element from array of objects.

Can a property of an object be an array?

Just as object properties can store values of any primitive data type (as well as an array or another object), so too can arrays consist of strings, numbers, booleans, objects, or even other arrays.


2 Answers

$.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);
    });
});
like image 144
Strille Avatar answered Oct 30 '22 22:10

Strille


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]);
	}
}
like image 34
VIJAYABAL DHANAPAL Avatar answered Oct 30 '22 20:10

VIJAYABAL DHANAPAL