/    Sign up×
Community /Pin to ProfileBookmark

How to create a multidimensional array with an .each() loop ?

Hello,

How can you create a multidimensional array with an each() loop?

The each function has to loop trough the .ui-selected object, and put all id’s and the offset in an array …

I hope somebody can help me because I’m searching all afternoon to fix this.

This is the code I was playing with .

[CODE]function sendAjax() {

var files = new Array();

$(‘.ui-selected’).each(function(index) {

var id = $(this).attr(‘id’);
var offset = $(this).offset();

files[index] = new Array();
files[index][‘id’] = id;
files[index][‘top’] = offset.top;
files[index][‘left’] = offset.left;

})

$.post(‘/testing/’, {‘files’: files},
function(data) {

// Testing purpose
alert(data);

}, ‘json’);
}
sendAjax();
[/CODE]

Thanks!

Christophe

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Christophe27authorOct 26.2010 — Found it! :-)
Copy linkTweet thisAlerts:
@toltmannsJan 17.2011 — I am looking for very similar functionality. Care to share your find?
Copy linkTweet thisAlerts:
@Christophe27authorJan 18.2011 — What do you need exactly?
Copy linkTweet thisAlerts:
@toltmannsJan 18.2011 — Basically trying to map out the dom tree and set the elements into a multidemensional array. From there, I would be recompiling the arrays to build out a select box with appropriate space offsets and ordering to show the dom tree. In my project, this would be used to send a new chunk of html code into a chosen dom element.

Here is some pseudo/non functioning code:

getAllChildren : function ( parent_element, levels_deep ){
levels_deep = typeof( levels_deep ) != 'undefined' ? levels_deep : 0;
//dom_tree = new Array();
//$( parent_element ).children()

// console.log(levels_deep + ' - ' + parent_element);

//dom_tree[ levels_deep ] = new Array( parent_element );

these_children = $( parent_element ).children();
// console.log(these_children);

these_children.each( function( index ){

if($(this) != ''){

// dom_tree[ levels_deep ][ index ] = new Array( $(this) );

Brosho.getAllChildren( $( this ), index + levels_deep + 1 );

}

});

// return dom_tree;

}
×

Success!

Help @Christophe27 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...