Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果某个数组索引中存在值,如何签入JavaScript?_Javascript_Arrays - Fatal编程技术网

如果某个数组索引中存在值,如何签入JavaScript?

如果某个数组索引中存在值,如何签入JavaScript?,javascript,arrays,Javascript,Arrays,这是否适用于测试索引位置的值是否存在,或者是否有更好的方法: if(arrayName[index]==""){ // do stuff } 从概念上讲,JavaScript中的数组包含array.length元素,从array[0]开始直到array[array.length-1]。如果i介于0和array.length-1之间,则索引为i的数组元素被定义为数组的一部分。如果我不在这个范围内,它就不在数组中 所以从概念上讲,数组是线性的,从零开始,一直到最大值,

这是否适用于测试
索引位置的值是否存在,或者是否有更好的方法:

if(arrayName[index]==""){
     // do stuff
}

从概念上讲,JavaScript中的数组包含
array.length
元素,从
array[0]
开始直到
array[array.length-1]
。如果
i
介于
0
array.length-1
之间,则索引为
i
的数组元素被定义为数组的一部分。如果我不在这个范围内,它就不在数组中

所以从概念上讲,数组是线性的,从零开始,一直到最大值,没有任何机制在不存在条目的范围内存在“间隙”。要确定给定位置索引(其中索引为0或正整数)上是否存在值,只需使用

if (i >= 0 && i < array.length) {
  // it is in array
}

有趣的是,由于JavaScript的比较规则,我的最后一个示例可以优化为:

if (array[index] != null) {
  // The == and != operators consider null equal to only null or undefined
}  
我们不能这样做吗:

if(arrayName.length > 0){   
    //or **if(arrayName.length)**
    //this array is not empty 
}else{
   //this array is empty
}
如果您的意思是“Null”->它的元素为Null或等于“”,在这种情况下:在过滤所有“Null”元素后检查数组是否为空

if(!arr.clean().length){return 'is null'}
当然,在以下步骤之前添加清洁方法:

Array.prototype.clean=function(){return this.filter(function(e){return (typeof  e !=='undefined')&&(e!= null)&&(e!='')})}

如果数组[index]为空,请尝试此操作

if (array[index] != null) 

我建议创建如下函数:

function isEmptyEl(array, i) {
   return !(array[i]);
}
if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}
array.every(function(element) {return !!element;}); // returns true or false
let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3
if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}
if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}
你可以这样称呼它:

function isEmptyEl(array, i) {
   return !(array[i]);
}
if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}
array.every(function(element) {return !!element;}); // returns true or false
let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3
if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}
if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}
强制开发人员遵守isEmptyEl接口将捕获输入错误,例如未定义的arrayName或indexVal变量

(在使用Javascript编程时,通常最好进行防御性编程。)

如果未定义arrayName,则会引发如下错误:

Uncaught ReferenceError: arrayName is not defined
    at <anonymous>:2:15
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)
未捕获引用错误:未定义arrayName
两点十五分
在Object.InjectedScript.\u evaluateOn(:895:140)
在Object.InjectedScript.\u evaluateAndWrap(:828:34)
在Object.InjectedScript.evaluate(:694:21)
未定义的indexVal的类似结果

如果数组或索引值不存在,则会出现错误

对于有效输入,只有当arrayName[indexVal]满足以下任一条件时,才会得到true:

let arr = [0, 1, 2, 3, 4, 5]

delete arr[3]

console.log(arr)      // [0, 1, 2, empty, 4, 5]

console.log(arr[3])   // undefined
  • 空的
  • 未定义
  • 空字符串
  • 0
  • 假的

您可以使用Loadsh库更高效地执行此操作,如:

如果您有一个名为“pets”的数组,例如:

var pets = ['dog', undefined, 'cat', null];

console.log(_.isEmpty(pets[1])); // true
console.log(_.isEmpty(pets[3])); // true
console.log(_.isEmpty(pets[4])); // false

_.map( pets, (pet, index) => { console.log(index + ': ' + _.isEmpty(pet) ) });
['23', null, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false

['23', '', 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false

['23', true, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns true
要检查所有数组值是否为空值或未定义值,请执行以下操作:

var pets=['dog',未定义,'cat',null];
console.log(u.isEmpty(pets[1]));//真的
console.log(u.isEmpty(pets[3]));//真的
console.log(u.isEmpty(pets[4]));//假的
_.map(pets,(pet,index)=>{console.log(index+':'+\ i.isEmpty(pet))}

仅使用
.length
是不安全的,在某些浏览器中会导致错误。这里有一个更好的解决方案:

if(array && array.length){   
   // not empty 
} else {
   // empty
}
或者,我们可以使用:

Object.keys(__array__).length

这取决于“空”是什么意思

当您试图获取对象上没有该名称属性的属性的值时,您将获得值
未定义

稀疏数组就是这样:不是所有
0
array.length-1
之间的索引都存在

因此,您可以检查
数组[index]==未定义的

但是,属性
索引
可能存在一个
未定义的
值。如果要过滤掉这种情况,可以使用
in
运算符或
hasOwnProperty
,如中所述

如果您想考虑具有<代码>未定义或<代码> NUL/<代码>值的现有属性,则可以使用松散的比较<代码>数组[索引]=未定义或<代码>数组[索引]=NULL/<代码> < < /P>
如果知道数组不是稀疏的,可以将
索引
数组.length
进行比较。但为了安全起见,您可能希望确保
索引
确实是数组索引,请参见使用Lodash,您可以执行以下操作:

if(_.has(req,'documents')){
      if (req.documents.length)
      _.forEach(req.documents, function(document){
        records.push(document);
      });
} else {
}

if(uu.has(req,'documents'))
检查请求对象是否有名为
documents
的属性,如果它有该属性,下一步
if(req.documents.length)
将验证它是否为空数组,这样就可以继续执行其他类似
forEach
的操作

检查是否从未定义或是否已删除:

if(typeof arrayName[index]==="undefined"){
     //the index is not in the array
}
也适用于关联数组和删除某些索引的数组

要检查它是否从未定义、是否已删除或为null或逻辑空值(NaN、空字符串、false),请执行以下操作:


我使用laravel数据表遇到了这个问题。我在活动日志中存储了一个名为
properties
的JSON值,并希望根据该值显示一个按钮是否为空

datatables将其解释为一个数组(如果它是空的),将其解释为一个对象(如果它不是空的),因此,以下解决方案适合我:

render: function (data, type, full) {
    if (full.properties.length !== 0) {
        // do stuff
    }
}

对象没有长度属性。

短而通用的方法

如果要检查任何数组是否具有falsy值(如false、undefined、null或空字符串),可以像这样使用every()方法:

function isEmptyEl(array, i) {
   return !(array[i]);
}
if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}
array.every(function(element) {return !!element;}); // returns true or false
let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3
if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}
if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}
例如:

var pets = ['dog', undefined, 'cat', null];

console.log(_.isEmpty(pets[1])); // true
console.log(_.isEmpty(pets[3])); // true
console.log(_.isEmpty(pets[4])); // false

_.map( pets, (pet, index) => { console.log(index + ': ' + _.isEmpty(pet) ) });
['23', null, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false

['23', '', 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false

['23', true, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns true
如果需要获取falsy值的第一个索引,可以这样做:

function isEmptyEl(array, i) {
   return !(array[i]);
}
if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}
array.every(function(element) {return !!element;}); // returns true or false
let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3
if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}
if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}
如果您只需要检查给定索引的数组的falsy值,您可以这样做:

function isEmptyEl(array, i) {
   return !(array[i]);
}
if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}
array.every(function(element) {return !!element;}); // returns true or false
let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3
if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}
if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}

好吧,首先让我们看看如果JavaScript中不存在数组值会发生什么,那么如果我们有一个如下所示的数组:

const arr = [1, 2, 3, 4, 5];
现在我们检查索引5是否存在6

arr[5];
我们得到了
未定义

这基本上就是给我们答案,检查是否未定义的最佳方法,如下所示:

function isEmptyEl(array, i) {
   return !(array[i]);
}
if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}
array.every(function(element) {return !!element;}); // returns true or false
let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3
if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}
if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}
在这种情况下,最好不要这样做:

if(!arrayName[index]) {
  //Don't check like this..
}
因为假设我们有这个数组:

const arr = [0, 1, 2];
我们做到了:

if(!arr[0]) {
  //This get passed, because in JavaScript 0 is falsy
}
因此,正如您所看到的,即使存在0,它也不会被识别,几乎没有其他事情可以做到这一点,让您应用