Ojame
Ojame

Reputation: 169

Adding to a key identifier array in jQuery?

I have to have a result which looks like this:

            ({1: 'string', 2: 'anotherstring', 3: 'andanother'});

It's not limited to three, it could be a variety of lengths (although not above 10).

I'm grabbing data from a php file through a loop:

            success: function(data, status){
                $.each(data, function(i,item){
                    var variableName = '<h1>'+item.draw+'</h1>'
                    + '<p>'+item.date+'</p>';

                    output.append(variableName);
            });

Basically I would like to have the 'draw' and 'date' variables (eg a string "3123 2010-12-01") put into an array with 'key identifiers' as it's needed by the script. I'm not sure the best way to go about this. I know it has to be inside the loop, and i'm thinking using the .append might have something to do with it?

            ({1: '3123 2010-12-01', 2: '6410 2011-04-28', 3: '7129 2012-01-01'});

Upvotes: 0

Views: 304

Answers (1)

Marcelo Diniz
Marcelo Diniz

Reputation: 2499

In the example, map will be the final object you're looking for. There's probably a better way to write it though.

// data looks like: [ {draw: '1', date: 'date1'}, {draw: '2', date: 'date2'} ];
var map = {};
$.each(data, function(i, v) {
    map[v.draw] = v.date;
});
// do what you need with map here

Example: http://jsfiddle.net/tscr9/

Upvotes: 1

Related Questions