Bre*_*eak 69 javascript jquery underscore.js
有没有办法在Javascript中轻松编写这样的东西:
[1,2,3].times do {
something();
}
Run Code Online (Sandbox Code Playgroud)
任何可能支持某些类似语法的库可能吗?
更新:澄清 - 我希望something()每个数组元素迭代分别调用1,2和3次
ahr*_*ren 72
只需使用一个循环:
var times = 10;
for(var i=0; i < times; i++){
doSomething();
}
Run Code Online (Sandbox Code Playgroud)
nve*_*rba 47
可能的ES6替代方案.
Array.from(Array(3)).forEach((x, i) => {
something();
});
Run Code Online (Sandbox Code Playgroud)
并且,如果你想要它"分别被称为1,2和3次".
Array.from(Array(3)).forEach((x, i) => {
Array.from(Array(i+1)).forEach((x, i2) => {
console.log(`Something ${ i } ${ i2 }`)
});
});
Run Code Online (Sandbox Code Playgroud)
这似乎是创建初始数组的更优化方式.
Array.from({ length: 3 }, (x, i) => {
something();
});
Run Code Online (Sandbox Code Playgroud)
vin*_*yll 37
这个答案是基于Array.forEach,没有任何库,只是本机香草.
基本上要打something()3次,使用:
[1,2,3].forEach(function(i) {
something();
});
Run Code Online (Sandbox Code Playgroud)
考虑以下功能:
function something(){ console.log('something') }
Run Code Online (Sandbox Code Playgroud)
输出将是
something
something
something
Run Code Online (Sandbox Code Playgroud)
要完成这些问题,这里something()分别进行1,2和3次调用:
[1,2,3].forEach(i => Array(i).fill(i).forEach(_ => {
something()
}))
Run Code Online (Sandbox Code Playgroud)
[1,2,3].forEach(function(i) {
Array(i).fill(i).forEach(function() {
something()
})
}))
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,输出都将是
输出将是
something
something
something
something
something
something
Run Code Online (Sandbox Code Playgroud)
(一次,然后两次,然后3次)
ggo*_*zad 19
因为你提到了下划线:
假设f您要调用的函数:
_.each([1,2,3], function (n) { _.times(n, f) });
Run Code Online (Sandbox Code Playgroud)
会做的.例如,有了f = function (x) { console.log(x); },你将进入你的控制台:
0 0 1 0 1 2
Tho*_*Tho 15
我们可以使用lodash来完成这项工作:
_.each([1, 2, 3], function(item) {
doSomeThing(item);
});
//Or:
_.each([1, 2, 3], doSomeThing);
Run Code Online (Sandbox Code Playgroud)
或者如果你想做N次:
var n = 10;
_.times(n, function() {
doSomeThing();
});
//Or:
_.times(n, doSomeThing);
Run Code Online (Sandbox Code Playgroud)
参考此链接进行lodash安装
Oza*_*man 15
你也可以用如下解构做同样的事情
[...Array(3)].forEach( _ => console.log('do something'));
Run Code Online (Sandbox Code Playgroud)
或者如果你需要索引
[...Array(3)].forEach(( _, index) => console.log('do something'));
Run Code Online (Sandbox Code Playgroud)
Web*_*nan 14
简单的一会儿怎么样。
let times = 5;
while (times--) {
console.log(times+1)
}Run Code Online (Sandbox Code Playgroud)
有关其工作原理的参考资料:Falsy和Decrement (--)
编辑:如果有可能times在其他地方进行操作,那么使用它会更安全,times-- > 0而不是在低于以下值的times--情况下防止无限循环times0
如果你不能使用Underscorejs,你可以自己实现它.通过将新方法附加到Number和String原型,您可以这样做(使用ES6箭头函数):
// With String
"5".times( (i) => console.log("number "+i) );
// With number variable
var five = 5;
five.times( (i) => console.log("number "+i) );
// With number literal (parentheses required)
(5).times( (i) => console.log("number "+i) );
Run Code Online (Sandbox Code Playgroud)
您只需要创建一个函数表达式(无论名称),并将其分配给您想要访问它的任何属性名称(在原型上):
var timesFunction = function(callback) {
if (typeof callback !== "function" ) {
throw new TypeError("Callback is not a function");
} else if( isNaN(parseInt(Number(this.valueOf()))) ) {
throw new TypeError("Object is not a valid number");
}
for (var i = 0; i < Number(this.valueOf()); i++) {
callback(i);
}
};
String.prototype.times = timesFunction;
Number.prototype.times = timesFunction;
Run Code Online (Sandbox Code Playgroud)
fill所有带有undefinedso map方法的项都可以工作:
Array.fill没有IE支持
Array(5).fill().map(()=>{
// Do this 5 times:
console.log(111)
})Run Code Online (Sandbox Code Playgroud)
for( let i=5; i--; ){
// Do this 5 times:
console.log(222)
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
95477 次 |
| 最近记录: |