2

I have a JSON file "icon.json" which contains:

[
    {
        "name": "sad",
        "url": "1.gif"
    },
    {
        "name": "smile",
        "url": "2.gif"
    },
    {
        "name": "smile2",
        "url": "3.gif"
    }
]

And I load it with AJAX in my html file

var c1 = [];
var c2 = [];
function testget(){
    $.ajax({
        url: 'icon.json',
        type: 'GET'
    })
    .done(function(msg) {
        // how to convert msg to array c1 and c2
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
}
$(document).ready(function() {
    testget();
});

Please tell me how to convert the object returned from the AJAX call to array c1 and c2 like this:

c1 = ["sad", "smile", "smile2"];
c2 = ["1.gif", "2.gif", "3.gif"];

1 Answer 1

8

You can do this with a simple loop:

.done(function(msg) {
    for (var i = 0; i < msg.length; i++) {
        c1.push(msg[i].name);
        c2.push(msg[i].url);
    }
})

Example fiddle

Sign up to request clarification or add additional context in comments.

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.