Iterate Over Object Literal Values

Iterate over Object Literal Values

var obj = {
'foo': 1,
'bar': 2
};

for (var key in obj) {
console.log(obj[key]);
}

Or with jQuery:

$.each(obj, function(key, value) {
console.log(this, value, obj[key]);
});

Iterate through properties of a object literal

You are accessing your object values wrong.

Change

function displayRecipes() {
var recipes = getRecipes();
for (var key in recipes) {
if (!$('#rec').val()) {
$('#recipeList').add('<ul id="#rec">' + key + '</ul>').appendTo(document.body);
$('#rec').add('<li>' + key.recipeType + '</li>').appendTo(document.body);
$('#rec').add('<li>' + key.recipePersonsCount + '</li>').appendTo(document.body);
$('#rec').add('<li>' + key.recipeTime + '</li>').appendTo(document.body);
$('#rec').add('<li>' + key.recipeContents + '</li>').appendTo(document.body);
$('#rec').add('<li>' + key.recipeInstructions + '</li>').appendTo(document.body);
}
}
};

to

function displayRecipes() {
var recipes = getRecipes();
for (var key in recipes) {
if (!$('#rec').val()) {
$('#recipeList').add('<ul id="#rec">' + recipes[key] + '</ul>').appendTo(document.body);
$('#rec').add('<li>' + recipes[key].recipeType + '</li>').appendTo(document.body);
$('#rec').add('<li>' + recipes[key].recipePersonsCount + '</li>').appendTo(document.body);
$('#rec').add('<li>' + recipes[key].recipeTime + '</li>').appendTo(document.body);
$('#rec').add('<li>' + recipes[key].recipeContents + '</li>').appendTo(document.body);
$('#rec').add('<li>' + recipes[key].recipeInstructions + '</li>').appendTo(document.body);
}
}
};

How do I loop through or enumerate a JavaScript object?

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};

for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}

Looping through object literal with nested functions

You are not checking for the key, you are checking a string against a function.

for(var key in functions){
if (key==="foo") {
console.log("hello");
}
}

another way is to use Object.keys()

var keys = Object.keys(functions);
if (keys.indexOf("foo")>-1) {
console.log("hello");
}

How do I iterate over an object literal array to find a matching property?

You can reduce regularListings and calculate the totals

const regularListings = [
listing('detergent', 10),
listing('hennessey', 100),
listing('carlo rozzi', 20),
listing('coffee', 5),
listing('cookies', 6),
listing('mountain dew', 2)
]

const total = regularListings.reduce((state, item) => state + item, 0);

How do I iterate over object literal array with jquery $.each() method?

Try:

   $.each(
obj,
function(i, val){
console.log(val.className + ', ' + val.url);
}
);

The $.each function parameter takes two arguments -- the index of the current item in the array, and the second is the actual current value of the array.

See the API for some more explanation and examples.

You can see this fiddle to see it in action.



Related Topics



Leave a reply



Submit