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
Onclick Event Getting Called Automatically
Is Reading the 'Length' Property of an Array Really That Expensive an Operation in JavaScript
Regex Created via New Regexp(Mystring) Not Working (Backslashes)
Get Loop Counter/Index Using For…Of Syntax in JavaScript
Why Do We Need to Install Gulp Globally and Locally
How to Perform Flood Fill with HTML Canvas
Javascript: Check If Mouse Button Down
Break Promise Chain and Call a Function Based on the Step in the Chain Where It Is Broken (Rejected)
JavaScript - Convert Array of Arrays into Array of Objects with Prefilled Values
Es6 Promise.All() Error Handle - Is .Settle() Needed
Convert Time Interval Given in Seconds into More Human Readable Form
Remove All Special Characters Except Space from a String Using JavaScript
How to Use Nodejs to Open Default Browser and Navigate to a Specific Url
Firestore: What's the Pattern for Adding New Data in Web V9