Why not do two passes, since the array will have a one-to-one
correspondence with the divs in the containing div?

$(document).ready(function(){
    var arr = [{content: 'some html 1', data: 'some data 1'},{content:
'some html 2', data: 'some data 2'},{content: 'some html N', data:
'some data N'}];
    $('<div>').append($.map(arr,function(a){return '<div>'+a.content
+'</div>';}).join('')).children('div').each(function(i,a){$(a).data
('testValue',arr[i].data)}).end().appendTo('body');
});

On Mar 23, 8:26 pm, sliver <sliver2...@gmail.com> wrote:
> Hi all,
>
> Ive been exploring various solutions for the following and was
> interested to hear other opinions:
>
> From AJAX call: "[{content: 'some html 1', data: 'some data 1'},
> {content: 'some html 2', data: 'some data 2'}, ... , {content: 'some
> html N', data: 'some data N'}]"
>
> Desired: Take the array from above, wrap the content values in a div
> and then attach the data to the div. After this, set the new divs to
> be the content of another element.
>
> My first thought was to put the array through a map:
> $.map(ARRAY, function(a) {
>         return $('<div>').html(a.content).data('testValue', a,data);
>
> });
>
> however, you can't simply just take the output from that and use it as
> an argument in another element's html method:
> // Won't work
> $('<div>').html($.map(ARRAY, function(a) { return $('<div>').html
> (a.content).data('testValue', a,data);}));
>
> Ive been working through a few solutions, none of which I find to be
> very elegant... so, any thoughts on an elegant solution out there?

Reply via email to