Cur*_*arn 259 javascript arrays repeat
在Python中,[2]
列表在哪里,以下代码给出了这个输出:
[2] * 5 # Outputs: [2,2,2,2,2]
Run Code Online (Sandbox Code Playgroud)
使用JavaScript中的数组是否存在一种简单的方法?
我写了以下函数来做到这一点,但是有更短或更好的东西吗?
var repeatelem = function(elem, n){
// returns an array with element elem repeated n times.
var arr = [];
for (var i = 0; i <= n; i++) {
arr = arr.concat(elem);
};
return arr;
};
Run Code Online (Sandbox Code Playgroud)
Nik*_*nen 556
在ES6中使用Array fill()方法
Array(5).fill(2)
//=> [2, 2, 2, 2, 2]
Run Code Online (Sandbox Code Playgroud)
Jan*_*sen 138
>>> Array.apply(null, Array(10)).map(function(){return 5})
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
>>> //Or in ES6
>>> [...Array(10)].map((_, i) => 5)
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
Run Code Online (Sandbox Code Playgroud)
Viv*_*vek 75
你可以试试:
Array(6).join('a').split(''); // returns ['a','a','a','a','a'] (5 times)
Run Code Online (Sandbox Code Playgroud)
更新(01/06/2018):
现在你可以重复一组字符了.
new Array(5).fill('a'); // give the same result as above;
// or
Array.from({ length: 5 }).fill('a')
Run Code Online (Sandbox Code Playgroud)
注意:有关兼容性和浏览器支持,请查看有关fill(...)和(...)的更多信息.
Guf*_*ffa 49
你可以这样做:
function fillArray(value, len) {
if (len == 0) return [];
var a = [value];
while (a.length * 2 <= len) a = a.concat(a);
if (a.length < len) a = a.concat(a.slice(0, len - a.length));
return a;
}
Run Code Online (Sandbox Code Playgroud)
它在每次迭代中使数组加倍,因此它可以创建一个具有很少迭代的非常大的数组.
注意:您也可以通过使用push
而不是改进您的功能concat
,因为concat
每次迭代都会创建一个新数组.像这样(仅作为如何使用数组的示例):
function fillArray(value, len) {
var arr = [];
for (var i = 0; i < len; i++) {
arr.push(value);
}
return arr;
}
Run Code Online (Sandbox Code Playgroud)
Fel*_*aro 47
以前在知道这个之前都是手动写的???
Array(5).fill('') => ['','','','','']
Array(3).fill({ value: 2 }) => [{ value: 2 },{ value: 2 },{ value: 2 }]
Run Code Online (Sandbox Code Playgroud)
您还可以使用 fill() + map() 轻松创建顺序数组
Array(4).fill('').map((_, i) => i + ' ') => ['0 ','1 ','2 ','3 ']
Array(3).fill(' ').map((flower, i) => i + flower) => ['0 ','1 ','2 ']
Run Code Online (Sandbox Code Playgroud)
Tho*_*son 31
Array.from({length:5}, i => 1) // [1, 1, 1, 1, 1]
或创建具有增加值的数组
Array.from({length:5}, (e, i)=>i) // [0, 1, 2, 3, 4]
Dro*_*ans 29
在lodash它不是那么糟糕:
_.flatten(_.times(5, function () { return [2]; }));
// [2, 2, 2, 2, 2]
Run Code Online (Sandbox Code Playgroud)
编辑:更好:
_.times(5, _.constant(2));
// [2, 2, 2, 2, 2]
Run Code Online (Sandbox Code Playgroud)
编辑:更好:
_.fill(Array(5), 2);
Run Code Online (Sandbox Code Playgroud)
eto*_*xin 29
如果需要重复数组,请使用以下命令。
Array(3).fill(['a','b','c']).flat()
Run Code Online (Sandbox Code Playgroud)
将返回
Array(9) [ "a", "b", "c", "a", "b", "c", "a", "b", "c" ]
Run Code Online (Sandbox Code Playgroud)
bro*_*ong 15
[c] * n
可以写成:
Array(n+1).join(1).split('').map(function(){return c;})
Run Code Online (Sandbox Code Playgroud)
因此对于 [2] * 5
Array(6).join(1).split('').map(function(){return 2;})
Run Code Online (Sandbox Code Playgroud)
Shm*_*dty 10
您还可以像这样扩展Array的功能:
Array.prototype.fill = function(val){
for (var i = 0; i < this.length; i++){
this[i] = val;
}
return this;
};
// used like:
var arry = new Array(5)?.fill(2);
// or
var arry = new Array(5);
arry.fill(2);
?console.log(arry);? //[2, 2, 2, 2, 2]
Run Code Online (Sandbox Code Playgroud)
我应该注意,如果您使用第三方库,扩展内置对象的功能可能会导致问题.始终将这一点权衡到你的决定中
在 Node.js REPL 中:
> Array.from({length:5}).map(x => 2)
[ 2, 2, 2, 2, 2 ]
Run Code Online (Sandbox Code Playgroud)
如果您需要多次重复数组:
var arrayA = ['a','b','c'];
var repeats = 3;
var arrayB = Array.apply(null, {length: repeats * arrayA.length})
.map(function(e,i){return arrayA[i % arrayA.length]});
// result: arrayB = ['a','b','c','a','b','c','a','b','c']
Run Code Online (Sandbox Code Playgroud)
受到这个答案的启发
使用这个功能:
function repeatElement(element, count) {
return Array(count).fill(element)
}
Run Code Online (Sandbox Code Playgroud)
>>> repeatElement('#', 5).join('')
"#####"
Run Code Online (Sandbox Code Playgroud)
或者对于更紧凑的版本:
const repeatElement = (element, count) =>
Array(count).fill(element)
Run Code Online (Sandbox Code Playgroud)
>>> repeatElement('#', 5).join('')
"#####"
Run Code Online (Sandbox Code Playgroud)
或者对于可咖喱的版本:
const repeatElement = element => count =>
Array(count).fill(element)
Run Code Online (Sandbox Code Playgroud)
>>> repeatElement('#')(5).join('')
"#####"
Run Code Online (Sandbox Code Playgroud)
您可以将此函数与列表一起使用:
const repeatElement = (element, count) =>
Array(count).fill(element)
>>> ['a', 'b', ...repeatElement('c', 5)]
['a', 'b', 'c', 'c', 'c', 'c', 'c']
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
181369 次 |
最近记录: |