简体   繁体   中英

Creating array of empty strings?

Is there an easy way to create an array of empty strings in javascript? Currently the only way I can think to do it is with a loop:

var empty = new Array(someLength);
for(var i=0;i<empty.length;i++){
    empty[i] = '';
}

but I'm wondering if there is some way to do this in one line using either regular javascript or coffeescript.

Update: on newer browsers - use .fill : Array(1000).fill('') will create an array of 1000 empty strings.


Yes, there is a way:

 var n = 1000;
 Array(n).join(".").split("."); // now contains n empty strings.

I'd probably use the loop though, it conveys intent clearer.

function repeat(num,whatTo){
    var arr = [];
    for(var i=0;i<num;i++){
        arr.push(whatTo);
    }
    return arr;
}

That way, it's perfectly clear what's being done and you can reuse it.

here's a simpler way using generic protos on Array and String:

"".split.call(Array(1001), ",")

EDIT: There's now even simpler ways, some of which are readable:

Array(1000).fill("");

" ".repeat(999).split(" ");

您可以获得一个定义大小的数组并用一些标记填充它:

const arr = Array(size).fill("");

You can try to do it by this way:

let n = 1000;
var emptyStrings = [...Array(n)].map(() => '')

Using Array.from ;

 const n = 5; const arr = Array.from({length: n}).map(el => "") console.log(arr)

You could make a function out of it:

function stringArray(length) {
    var arr = [];
    for(var i = 0; i < length; ++i) { arr.push(''); }
    return arr;
}

You could do something like this:

var someLength = 10;
var empty = Array.apply(0, Array(someLength)).map(function(){return '';});
// result: ["", "", "", "", "", "", "", "", "", ""]

只是为了好玩

var empty = Array.apply(null, new Array(someLength)).map(String.prototype.valueOf,"");

The easiest thing to do in CoffeeScript is to use a loop comprehension :

a = ('' for i in [0 ... someLength]) # Note: 3 dots
a = ('' for i in [1  .. someLength]) # Or 2 dots and start at 1
#...

Demo: http://jsfiddle.net/ambiguous/b9Vj9/

Although not widely available, once browsers start supporting EcmaScript 6 array comprehensions, you will be able to do something along the lines of:

var n = 1000;
var empty_strs = ['' for (x of new Array(n))]

Easy enough.

  1. Array with a length of 10 items, filled with empty strings ( "" ):

     Array.from({length: 10}).map( _ => "" ); // Array(10) [ "", "", "", "", "", "", "", "", "", "" ]
  2. Array with a length of 10 items, filled with numbers from 0 to 9:

     Array.from({length: 10}).map( (_, index) => index ); // Array(10) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  3. Array with a length of 10 items, filled with numbers from 1 to 10:

     Array.from({length: 10}).map( (_, index) => index + 1 ); // Array(10) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
  4. Array with a length of 10 items, filled with string containing "Chapter (1... 10)." :

     Array.from({length: 10}).map( (_, index) => `Chapter ${index + 1}.` ); // Array(10) [ "Chapter 1.", "Chapter 2.", "Chapter 3.", ... ]

(Using _ here as we don't use the value anyway.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM