如何检查是否一个多维数组项目在JS设置? [英] How to check if a multidimensional array item is set in JS?

查看:113
本文介绍了如何检查是否一个多维数组项目在JS设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查是否一个多维数组项目在JS设置?

How to check if a multidimensional array item is set in JS?

w[1][2] = new Array;
w[1][2][1] = new Array;
w[1][2][1][1] = 10; w[1][2][1][2] = 20; w[1][2][1][4] = 30;

如何检查是否 W [1] [2] [1] [3] 设置?

如果(typeof运算W [1] [2] [1] [3]!='不确定')不起作用。

我不希望使用一个对象而不是数组。

I don't want to use an Object instead of Array.

推荐答案

您不检查检查其孩子为孩子之前的previous数组元素存在的元素不能存在如果父犯规

You are not checking the previous array elements existence before checking its children as the children elements cant exist if the parent doesnt

if( 
    typeof(w) != 'undefined' &&
    typeof(w[1]) != 'undefined' &&
    typeof(w[1][2]) != 'undefined' &&
    typeof(w[1][2][1]) != 'undefined' &&
    typeof(w[1][2][1][3]) != 'undefined' &&
  ) {
    //do your code here if it exists  
  } else {
    //One of the array elements does not exist
  }

如果本将运行其他子句中的code。如果它看到任何previous元素不存在的。它停止检查其他人是否有任何preceding检查返回false。

The if will run the code in the else clause if it sees any of the previous elements not existing. It stops checking the others if any of the preceding checks returns false.

这篇关于如何检查是否一个多维数组项目在JS设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆