5

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?

A Super Awesome
  • 93
  • 1
  • 1
  • 7
  • Please note that the problem has **nothing** to do with JSON at all. It seems you are confusing JavaScript object/array literals (constructs of the JavaScript language syntax) with JSON (a language-independent data-exchange format, like XML or CSV). I will edit your question accordingly. See also: [There is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Felix Kling Oct 03 '13 at 08:23
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Oct 03 '13 at 08:25

3 Answers3

8
$.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);
    });
});
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Strille
  • 5,741
  • 2
  • 26
  • 40
  • i forgot, it should be, how can i view the values of outlet_name by not using item.outlet_name. – A Super Awesome Oct 03 '13 at 08:27
  • this is confusing im sorry, i mean how can i view the outlet_name or the second object, because the the key names are randomly generated. like having [1] after the key name – A Super Awesome Oct 03 '13 at 08:47
  • It sounds like the data structure is confusing :-) If you have an object with unknown/random property names, you can't really access a specific property without knowing the name. It's not like an array where you can access a value by index ("pick the first value"). – Strille Oct 03 '13 at 09:04
2

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

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!

Leonardo Wildt
  • 2,529
  • 5
  • 27
  • 56