【问题标题】:Check if value exists in random n-dimensional array Javascript检查随机n维数组Javascript中是否存在值
【发布时间】:2015-02-24 10:29:11
【问题描述】:

我正在编写一个检查 n 维数组是否具有特定值的函数:

function checkVal(array, value) {
  if (value exists) {
    return true;
  } else {
    return false;
  }
}

我的问题是,我希望此函数适用于任何数组,而不管其维数或元素类型如何。我试图首先展平阵列,但只能在几个维度上做到这一点。

编辑: 一些可能的数组示例:

var arr1 = ['1','3',['a','b'],'4,5'];

var arr2 = ['a','b',['c','d',['e',['e',['e',['e',['e',['e',['e',['e',['e',['e',['f',['f',['f',['f',['f',['f',['g',['g',['g',['g',['g',['g',['g',['h']]]]]]]]]]]]]]]]]]]]]]]]]];

【问题讨论】:

  • 请添加一些数组的案例...

标签: javascript arrays multidimensional-array


【解决方案1】:

无需展平阵列,这只是额外的工作。你需要的是一个递归函数:

function checkVal(array, value) {
    // `Array#some` loops through the array until the iterator
    // function returns true; it returns true if the iterator
    // does at some point, false otherwise
    return array.some(function(entry) {
        // If this entry in the array is an array, recurse
        if (Array.isArray(entry)) {
            return checkVal(entry, value);
        }

        // It isn't, do an equality check
        return entry === value;
    });
}

Array#someArray.isArray 都是 ES5 函数,因此存在于任何现代浏览器中,并且两者都可以为 IE8 等较旧的浏览器进行填充/填充。或者,当然,可以用无聊的for 循环和旧的Object.prototype.toString.call(entry) === "[object Array]" 测试来重写上面的内容,以测试某物是否为数组。

请注意,我使用=== 进行相等检查。如果您需要更复杂的东西,例如对象等价而不是身份等,请在此处进行更改。

示例/基本测试:

function checkVal(array, value) {
  // `Array#some` loops through the array until the iterator
  // function returns true; it returns true if the iterator
  // does at some point, false otherwise
  return array.some(function(entry) {
    // If this entry in the array is an array, recurse
    if (Array.isArray(entry)) {
      return checkVal(entry, value);
    }

    // It isn't, do an equality check
    return entry === value;
  });
}

snippet.log(checkVal(['a', 'b', 'c'], 'b'));         // true
snippet.log(checkVal(['a', 'b', 'c'], 'd'));         // false

snippet.log(checkVal([['a'], ['b', 'c']], 'c'));     // true
snippet.log(checkVal([['a'], ['b', 'c']], 'd'));     // false

snippet.log(checkVal([['a'], [['b', ['c']]]], 'c')); // true
snippet.log(checkVal([['a'], [['b', ['c']]]], 'd')); // false
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

【讨论】:

    【解决方案2】:

    您需要一个递归函数来签入数组的所有子项

    function searchValue(arr, val){
      // found another array
      if(arr instanceof Array){
        for(var i=0; i<arr.length; i++){
          if(searchValue(arr[i],val)){
            return true; // stop on first valid result
          }
        }
        return false;
      }else{
        // found a leaf
        return arr == val; // <-- if you want strict check, use ===
      }
    }
    
    if(searchValue(myArray, 'my variable')){
      // do something..
    }
    

    它适用于任何浏览器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多