使用Array.every()函数检查是否定义了所有值
Using Array.every() function to check if all values are defined
我正在尝试检查我的所有值是否都定义在我的数组中。我的代码是以下
var check = function (item){return item !== undefined;};
array.every(check);
我在以下阵列上尝试过:
var array = [];
array[5] = 0; //[undefined × 5, 0]
array.every(check); //return true although there are 5 udefined values there
我做错了什么?
如上所述,every
跳过"孔"。
如果你真的想要这个功能,那么你可以添加这个简单的方法:
Array.prototype.myEvery= function (pred) {
for (var i = 0; i < this.length; i++) {
if (!pred(this[i])) return false;
}
return true;
}
MDN为Array.every
提供了polyfill(读取:确切的代码)。如果我们只是修改它以删除对属性存在的检查,这很容易:
if (!Array.prototype.reallyEvery) {
Array.prototype.reallyEvery = function(callbackfn, thisArg) {
'use strict';
var T, k;
if (this == null) {
throw new TypeError('this is null or not defined');
}
// 1. Let O be the result of calling ToObject passing the this
// value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method
// of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (typeof callbackfn !== 'function') {
throw new TypeError();
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0.
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// i. Let kValue be the result of calling the Get internal method
// of O with argument Pk.
kValue = O[k];
// ii. Let testResult be the result of calling the Call internal method
// of callbackfn with T as the this value and argument list
// containing kValue, k, and O.
var testResult = callbackfn.call(T, kValue, k, O);
// iii. If ToBoolean(testResult) is false, return false.
if (!testResult) {
return false;
}
k++;
}
return true;
};
}
我刚刚把它重命名为Array.reallyEvery()
。
这是因为.every()
方法在调用check()
函数之前会检查该值是否存在,因此check()
将只为最后一个元素(0
)调用。
注意:请记住,如果函数返回false
值,则.every()
方法将停止。
如果你想检查,试试这个:
var array = [1, 2, undefined, undefined, 1];
var check = function (item){return item !== undefined;};
array.every(check)
// this will work for 1 and 2, but will stop at the third element
array.every(function(item) {console.log(item); return true;});
// this will log all the elements, because the function always returns true
请尝试以下操作。
Array.prototype.every = function(){
for(var i=0;i<this.length;i++){
if(this[i]==undefined)
return false;
}
return true;
}
var array = [];
array[5] = 0;
console.log(array.every());
因为数组的长度是预先确定的,所以我使用filter函数绕过了它。
var check = function (item) {return item !== undefined};
array.filter(check).length === predeterminedLength;
谢谢大家的回答!!一如既往的伟大社区
相关文章:
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 用于检查数组中是否存在元素的javascript自定义方法
- 是否排除节点中错误堆栈的第一行?/节点中的自定义错误类型
- 如何在JavaScript中检查变量是否为null和/或未定义
- 检查对象的所有属性是否未定义
- 如何查看变量名称是否已在Javascript中定义
- 注册后是否可以定义HTML5自定义元素中的生命周期回调
- 是否可以替换或自定义getUserMedia提示
- 是否可以在定义对象时访问对象值
- 是否可以在AlloyUI中拥有自定义连接器
- 是否可以通过Primefaces 3.3的自定义对话框取消Ajax请求
- AngularJs 检查是否未定义和空的最佳实践
- 如何检查服务器是否返回未定义然后忽略它
- 我是否必须在每个文件中引用 TypeScript 定义
- 如何确定控件/插件是否从 CRM 2011 中功能区上的自定义按钮触发
- 检查未定义是否不起作用
- 这些JavaScript包含定义是否有区别
- angular是如何定义是否应该使用某个编译器的?
- 检查变量(定义)是否在JavaScript中不起作用
- Raphael / SVG路径定义是否总是以'M'开头?(如果不是,这样的路径的原点是否总是0,0?)