I am having an array that consists the objects with a key, value how can we iterate each object for caste
and id
.
[
Object {
The forEach loop accepts an iterator function and, optionally, a value to use as "this" when calling that iterator function.
var donuts = [
{ type: "Jelly", cost: 1.22 },
{ type: "Chocolate", cost: 2.45 },
{ type: "Cider", cost: 1.59 },
{ type: "Boston Cream", cost: 5.99 }
];
donuts.forEach(function(theDonut, index) {
console.log(theDonut.type + " donuts cost $"+ theDonut.cost+ " each");
});
Subsequently it can also be broken down to this
var donuts = [
{ type: "Jelly", cost: 1.22 },
{ type: "Chocolate", cost: 2.45 },
{ type: "Cider", cost: 1.59 },
{ type: "Boston Cream", cost: 5.99 }
];
function ShowResults(donuts) {
console.log(donuts.type + " donuts cost $"+ donuts.cost+ " each");
}
donuts.forEach(ShowResults);