Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 12:21 PM   PM User | #1
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
easy numbered array generation

i have been search forever for a way to make a numbered array using only native functions and without looping.

i had figured out a long, convoluted way of doing it with eval and bind(), but it was not pretty.

the one trick to this native functionality is that you have to specify a number one more than the ceiling you want, but that's not the end of the world...

the method is related to the popular String repeat trick:
Code:
Array(101).join(",").split(",").join("hello world \t")
you can even shorten the code below by removing ".map(Number)" if you don't need "real Number" numbers, but simply a list of digits.

here is the numbered array code:

Code:
Object.keys(  new String(  Array(101)  )    ).map(Number)
result
Code:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

for you cut-and-paste pleasure, here are some working reusables:


as a function
Code:
function arr(n) {
	return Object.keys(new String(Array(n - -1))).map(Number);
}
usage
Code:
arr(10) // === [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

arr(12).map(arr).join("\n") /* ==="

0
0,1
0,1,2
0,1,2,3
0,1,2,3,4
0,1,2,3,4,5
0,1,2,3,4,5,6
0,1,2,3,4,5,6,7
0,1,2,3,4,5,6,7,8
0,1,2,3,4,5,6,7,8,9
0,1,2,3,4,5,6,7,8,9,10 " 

*/



as a mutating array prototype
Code:
Array.prototype.numbered = function _numbered(n) {
	n = n || this.length;
	this.length = 0;
	this.push.apply(this, Object.keys(new String(Array(n - -1))).map(Number));
	return this;
};//end [].numbered()

and as a hidden mutating array prototype

Code:
Object.defineProperty(Array.prototype,"numbered", function _numbered(n) {
	n = n || this.length;
	this.length = 0;
	this.push.apply(this, Object.keys(new String(Array(n - -1))).map(Number));
	return this;
});//end [].numbered()

usage
Code:
["a","b","c"].numbered() // === [0, 1, 2]

["a","b","c"].numbered(10) // === [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Array().numbered(10) // === [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
Dormilich (10-11-2012)
Old 10-11-2012, 07:46 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
should work equally well:
PHP Code:
function arr(n) {
    return 
Object.keys(new String(Array(+1))).map(Number);

__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 10-11-2012 at 08:45 AM..
Dormilich is offline   Reply With Quote
Old 10-14-2012, 06:40 AM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Dormilich View Post
should work equally well:
PHP Code:
function arr(n) {
    return 
Object.keys(new String(Array(+1))).map(Number);

i figured out an even shorter way:
PHP Code:
function arr(n) {
    return 
Object.keys(Object(Array(n)+"r")).map(Number);
}

arr(5) +""// === "0,1,2,3,4" 
it uses a one-char string to both coerce the array and pad the length to the desired count.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:11 PM.


Advertisement
Log in to turn off these ads.