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.