Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

GetElementsAt //JavaScript Function

DZone's Guide to

GetElementsAt //JavaScript Function

·
Free Resource
This function is able to get all the elements in a specific level of the array

example:


x = [-1, [[8, 22, -7], 32, [[4, 10, -2], 122]], 13];
alert(x.getElementsAt(3));



code:


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
Array.prototype.getElementsAt = function(level){
	var r = [], x = [], c = this, l = level, i;
	do
		if(i = c.length, !l)
			for(; i; r[r.length] = c[--i]);
		else
			for(; i; c[--i] instanceof Array && x.push(c[i], l - 1));
	while(l = x.pop(), (c = x.pop()) != undefined);
	return r;
}
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}