开发者

Help to implement if-statement with array values

I have a following source code php+js: http://pastebin.org/277948

I want to rewrite it using pure JS, but can’t imagine the way.

Any advices are apprec开发者_JS百科iated.


You can use the every method to test if all elements in the array satisfy the predicate.

if (inp && vals.every(Boolean)) {
  // or:  vals.every(function(x){return x;})
  ...
}

But if you have to target browsers that does not support the every method, you can evaluate the condition with a for loop.

if (inp) {
   accept = true;
   for (var i = vals.length-1; i >= 0; -- i)
     if (!vals[i]) {
       accept = false;
       break;
     }
   if (accept) {
     ...
   }
}


sorry dont know php but this is the javascript.

if (inp == true) {
   for(i =0;i<numFields; i++){
     do something with vals[i];
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜