0

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.

7
  • 2
    that's an array of objects, btw. Commented May 9, 2013 at 18:06
  • I suppose this is a printout from a console or similar? Because it is not a valid object literal Commented May 9, 2013 at 18:07
  • Can you not use $.each(listOfObjects, function(index, obj){ to get each object and then for each of those, you can access obj.image, obj.heading, obj.text? Commented May 9, 2013 at 18:07
  • yup its a console output my object is like this [ { "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." } ] Commented May 9, 2013 at 18:08
  • My code is like this // Featcing all Information $.getJSON("javascript/mobile.json", function(data){ console.log(); //var image = $(data[image]); $.each(data, function(i, e) { console.log('image='+ i); }); }); Commented May 9, 2013 at 18:08

2 Answers 2

4

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);
});
Sign up to request clarification or add additional context in comments.

Comments

2

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.