Reputation: 2950
I have object like this
[Object {
image =
"images/item-1.png"
, heading =
"Careers"
, text =
"Lorem ipsum dolor sit a...ctetur adipiscing elit."
},
Object {
image =
"images/item-2.png"
, heading =
"Contact Us"
, text =
"Morbi tincidunt commodo scelerisque."
},
Object {
image =
"images/item-3.png"
, heading =
"About Us"
, text =
"Duis porttitor diam vitae leo elementum accumsan."
}]
How i can extract value from this object.?
I am trying to achieve this using $.each but not getting proper result. My requirement is to get image url, heading and text in separate variable. Pleas help me.
Upvotes: 0
Views: 91
Reputation: 14620
I don't like using $.each
as the jQuery overhead is massive compared to a simple for loop that can achieve the same thing.
for (var i = 0; i < arr.length; i++)
{
var image_url = arr[i].image,
heading = arr[i].heading,
text = arr[i].text;
// Do stuff with variables
}
Upvotes: 2
Reputation: 73896
You can simply do this:
var arr = [{
"image": "images/item-1.png",
"heading": "Careers",
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
}, {
"image": "images/item-2.png",
"heading": "Contact Us",
"text": "Morbi tincidunt commodo scelerisque."
}, {
"image": "images/item-3.png",
"heading": "About Us",
"text": "Duis porttitor diam vitae leo elementum accumsan."
}]
$.each(arr, function (index, value) {
var image_url = value.image;
var heading = value.heading;
var text = value.text;
console.log(index, image_url, heading, text);
});
Upvotes: 4