1

I want to type something like [1] * totalPoints so that if totalPoints was 3, it would give me an array of [1,1,1]. I can't think of what it would be called though.. as such, my searches have turned up nothing. I mean, I could easily accomplish this with a for loop, but I seem to be under the impression that I've used something like this before and I just can't think of it. Is there anything like this in javascript?

さりげない告白
  • 1,397
  • 2
  • 13
  • 26
  • I don't think JS has this built-in, you may be thinking of other languages. – Barmar Mar 01 '14 at 00:33
  • This has been asked before: http://stackoverflow.com/questions/4049847/initializing-an-array-with-a-single-value – user3358344 Mar 01 '14 at 00:34
  • possible duplicate of [Create an array with same element repeated multiple times in javascript](http://stackoverflow.com/questions/12503146/create-an-array-with-same-element-repeated-multiple-times-in-javascript) – Barmar Mar 01 '14 at 00:35
  • Alright, thanks. I know how I'm going to handle it now. It was probably python I did that in now that I think about it... – さりげない告白 Mar 01 '14 at 00:39

2 Answers2

2

Create a new array of size 3 then use the array map function on each element, calling valueOf:

var totalPoints = Array.apply(null, new Array(3)).map(Number.prototype.valueOf,1);

http://jsfiddle.net/pXgu4/

  • This is the closest to what I was looking for. totalPoints was the variable however, so it would go where the 3 is and the array would be named something else. Thanks for your answer. ^^ – さりげない告白 Mar 01 '14 at 00:49
  • 2
    Though this is nifty code, I would recommend against it. Really what's the reason to save those few lines of a loop but in return making a task as simple as this look so complicated? Anyone looking at this code will have trouble understanding what it does, including you in a few weeks (if you even understand how this works in the first place). Readability is more important than saving yourself a couple of lines. – Ingo Bürk Mar 01 '14 at 00:55
  • @IngoBürk and is the slowest option. A pre-allocated array filled with a for-loop is by far the fastest option (http://jsperf.com/pre-fill-an-array) –  Mar 01 '14 at 00:59
  • @Ken But that also doesn't matter. If you have to worry about the performance of creating an array with a few entries, you are in a lot of trouble. – Ingo Bürk Mar 01 '14 at 01:01
  • 1
    @IngoBürk: That all depends on context. Try playing with canvas for example. Every bit counts. – user13500 Mar 01 '14 at 01:02
  • @user13500 Good JS engines optimize stuff like loops so it doesn't really matter how exactly you write them. http://www.youtube.com/watch?v=goreks4apFg#t=3m30s – Ingo Bürk Mar 01 '14 at 01:05
  • To make sure I should be more precise: "doesn't matter" was the wrong term of me to use. Really what I meant to say is: it's the last thing to worry about. There are way more important reasons to decide against this. *If* you do have to create 300,000 arrays per second, yes, worry about loop performance. But even in a canvas environment that should be pretty rare and you likely will have more serious performance issues than this then. – Ingo Bürk Mar 01 '14 at 01:12
0
var myArray = [];

function pointsArray(totalPoints) {
    for (i = 0; i < totalPoints; i++) {
        myArray.push(1);
    }

    return myArray;
}

http://jsfiddle.net/isherwood/CvscV

Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
isherwood
  • 58,414
  • 16
  • 114
  • 157