1

It's been awhile since I've used JavaScript and I'm just trying to think of different ways to do things in JavaScript out of curiosity. Could someone tell me how I can output all the data in the cars object in a single function call? I know I can use dot notation to output particular values individually, but I want to know if/how I can get all of the values of each object in the cars object in the object in one shot. Is this possible?

var cars = [{
    car1: {
        "owner": "lois",
            "make": "chevy",
            "model": "malibu",
            "year": "2011",
            "color": "red"
    }
}, {
    car2: {
        "owner": "jeff",
            "make": "ford",
            "model": "focus",
            "year": "2002",
            "color": "silver"
    }
}, {
    car3: {
        "owner": "bob",
            "make": "chrysler",
            "model": "pacifica",
            "year": "2008",
            "color": "blue"
    }
}, {
    car4: {
        "owner": "sallie",
            "make": "toyota",
            "model": "corolla",
            "year": "2011",
            "color": "black"
    }
}];




function carOwners(obj_arr) {
    //this.ownerName2 = cars[1].owner;
    //this.ownerName3 = cars[2].owner;
    //this.ownerName4 = cars[3].owner;
    /* console.log("The vehicle owner's name are " + ownerName1 + ", " + ownerName2 + ", " + ownerName3 + ", and " + ownerName4 + ".");  */

    for (var i = 0, len = cars.length; i < len; i++) {
        for (var key in cars) {
            console.log(' name=' + key + ' value=' + cars[key]);
        }
    }

}

carOwners(cars);
//carOwners(cars[1].owner);
//carOwners(cars[2].owner);
//carOwners(cars[3].owner);

When I run that function I get the following results (open console in Chrome), but this is not what I want. I want to see the data for each (parse the object), not the type of data.

name=0 value=[object Object]
name=1 value=[object Object]
name=2 value=[object Object]
name=3 value=[object Object]
name=0 value=[object Object]
name=1 value=[object Object]
name=2 value=[object Object]
name=3 value=[object Object]
name=0 value=[object Object]
name=1 value=[object Object]
name=2 value=[object Object]
name=3 value=[object Object]
name=0 value=[object Object]
name=1 value=[object Object]
name=2 value=[object Object]
name=3 value=[object Object]

Thanks.


ADDED to explain why this is not duplicate: This is just my opinion, but from where I sit, my question is very simple: I want to output all values of an object in one fell swoop. I am not checking for missing values. It is very obvious in the other thread that the query is about checking for missing values, not outputting all values in one function call as I am asking how to do. In this thread I address the duplicate comparison in a comment below. I did not word my question to look for anything missing, and I know I wouldn't have read that thread to find my answer. Come to think of it, when I began typing my question in the Title box, that question (that you list for comparison) never appeared -- and I read every suggestion that was displayed in the title box for my answer, as well as on the right side of the page before taking the time to write all this out. I can't explain it any better than that. Thanks.

Chris22
  • 1,973
  • 8
  • 37
  • 55
  • This isn't JSON, it's a JavaScript object literal. – user229044 Sep 04 '15 at 22:59
  • possible duplicate of [Checking if an array key exists in a JavaScript object or array?](http://stackoverflow.com/questions/1098040/checking-if-an-array-key-exists-in-a-javascript-object-or-array) – Ryan Sep 04 '15 at 23:01
  • 1
    It doesn't seem to be a duplicate of that one – Luis Sieira Sep 04 '15 at 23:05
  • @ryan I looked over the question you list as possible dupe, but it doesn't appear (to me) to answer what I am asking. I want to output all values, I'm not checking for missing ones. I didn't see an answer that quite did that, but I could have missed it. Would you point it out to me directly? Thanks.. – Chris22 Sep 04 '15 at 23:06
  • @meagar I didn't realize that. I'll google the difference between javascript object literal and JSON. Is it the use of quotation marks or something in the syntax that makes the difference? – Chris22 Sep 04 '15 at 23:14
  • @Chris22 JSON is not a part of JavaScript. JSON is a textual encoding of data, like XML or CSV. – user229044 Sep 04 '15 at 23:23
  • @meagar yes, I understand that about encoding of data, xml with its tags, and CSV with it's delimiters JSON javascript object notation is easily "read/parsed" by JS. It's been a minute since I've read JSON docs, so to get more technical I'll have to re-read. Thanks. – Chris22 Sep 04 '15 at 23:37
  • wow, who's trying to close this question? That question listed did not even appear in the suggested answers title box when I was typing my question and it doesn't answer my question. I see very many duplicates on SO. What does close mean -- You will remove it? – Chris22 Sep 04 '15 at 23:42

3 Answers3

2

Not sure if this is exactly what you are asking, but you could do:

console.log(JSON.stringify(cars));
GPicazo
  • 6,516
  • 3
  • 21
  • 24
1

Use JSON.stringify(cars)

Alternatively, you can use cars.toString(), it will work unless it's some built-in function, but will not convert the nested objects.

There are subtle differences, but both should do what you expect

Community
  • 1
  • 1
Luis Sieira
  • 29,926
  • 3
  • 31
  • 53
1

Pass your array of cars into this function:

$.getJSON(your_variable, function (data){
  $.each(data, function(key, val) {
   for(var i in val) {
   console.log("owner is: " + val[i].owner);
   console.log("make is: " + val[i].make);
   console.log("model is " + val[i].model);
   console.log("year is " + val[i].year);
   console.log("color is " + val[i].color);
   }
  });
});
hhyder
  • 71
  • 6
  • thanks this is a different way. and I like the `for in` loop use. I'll run this to see how it works. – Chris22 Sep 04 '15 at 23:16