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.

8
  • This isn't JSON, it's a JavaScript object literal. Commented Sep 4, 2015 at 22:59
  • possible duplicate of Checking if an array key exists in a JavaScript object or array? Commented Sep 4, 2015 at 23:01
  • 1
    It doesn't seem to be a duplicate of that one Commented Sep 4, 2015 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.. Commented Sep 4, 2015 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? Commented Sep 4, 2015 at 23:14

3 Answers 3

2

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

console.log(JSON.stringify(cars));
Sign up to request clarification or add additional context in comments.

1 Comment

I'll try that. Thanks.
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);
   }
  });
});

1 Comment

thanks this is a different way. and I like the for in loop use. I'll run this to see how it works.
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

3 Comments

@meagar I tried that and this was logged: [object Object],[object Object],[object Object],[object Object] 16(index):70 [object Object],[object Object],[object Object],[object Object]
and console.log(JSON.stringify(cars))?
yes, that worked GPIcazo listed that solution as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.