简体   繁体   English

三维数组如何在JavaScript中使用“每个”函数

[英]How a three-dimensional array use 'every' function in javascript

how a three-dimension array use the every function, the result is strange: 三维数组如何使用every函数,结果很奇怪:

var foo = [];
foo[0] = [];
foo[1] = [];
foo[0][0] = [];
foo[0][1] = [];
foo[1][0] = [];
foo[1][1] = [];
foo[0][0][0] = "kanon";
foo[0][0][1] = "JOJO";
foo[0][1][0] = "Dio";
foo[0][1][1] = "Sort";
foo[1][0][0] = "What";
foo[1][0][1] = "Gary";
foo[1][1][0] = "Tofu";
foo[1][1][1] = "bili";
var foo2 = ["JOJO","JOJO","mon","ti","JOJO"];
function isJOJO(x){
    if(x === "JOJO"){
        document.write(x + " is JOJO. <br>");
        return true;
    }else{
        document.write(x + " is not JOJO. <br>");
        return false;
    }
}
document.write(foo.every(isJOJO));
document.write("<br><br>");
document.write(foo2.every(isJOJO));

the result in Chrome are following: Chrome中的结果如下:

kanon,JOJO,Dioo,Sort is not JOJO. 
false

JOJO is JOJO. 
JOJO is JOJO. 
mon is not JOJO. 
false

The result of the one-dimension array foo2 is correct, but the three-dimension array's result is not... 一维数组foo2的结果是正确的,但三维数组的结果不是...

Is the way that I define three-dimension array foo is wrong? 我定义三维数组foo的方式是否错误?

Why it just ordered until Sort , and even print kanon and Dioo ,which is not === "JOJO" 为什么只排序直到Sort ,甚至打印kanonDioo ,这不是===“ JOJO”

Or who can recommend me an another offical book or website, i am now using this book Object-Oriented JavaScript Second Edition authoritied by Stoyan Stefanov and Kumar Chetan Sharma 或者谁可以向我推荐另一本官方书籍或网站,我现在正在使用这本书,该书由Stoyan Stefanov和Kumar Chetan Sharma授权编写

Thanks a lot. 非常感谢。 ^w^ ^ w ^

Javascript doesn't have true multidimensional arrays, all arrays are 1-dimensional. Javascript没有真正的多维数组,所有数组都是一维的。 However, the elements of an array can be any type, and what you've done is created an array whose elements are other arrays, and those arrays also have arrays as elements. 但是,数组的元素可以是任何类型,创建的数组是其元素为其他数组的那些数组,这些数组也具有数组作为元素。

But array functions like Array.prototype.every only consider the top-level array they're given, they don't recurse into the contained arrays. 但是像Array.prototype.every这样的数组函数每一个都只考虑给定的顶级数组,而不会递归到所包含的数组中。 So when you try: 因此,当您尝试:

foo.every(isJOJO)

it calls 它呼吁

isJOJO(foo[0])

which is equivalent to 相当于

isJOJO([["kanon","JOJO"],["Dioo","Sort"]])

This is not equal to "JOJO" , so it returns false , and that causes every() to return false . 这不等于"JOJO" ,因此它返回false ,并导致every()返回false

Both arrays are actually one dimensional. 这两个阵列实际上都是一维的。 In fact, all arrays in all programming languages are one dimensional. 实际上,所有编程语言中的所有数组都是一维的。 The extra dimensions are just human interpretation of what the one dimensional array contains. 额外的维度只是人类对一维数组包含的内容的解释。

foo is a normal (one dimensional) array that happens to contain an array that contains an array. foo是一个普通(一维)数组,碰巧包含一个包含数组的数组。 Let's rewrite your code to make this obvious: 让我们重写您的代码以使其显而易见:

var foo = [];
foo[0] = [['kanon','JOJO'],['Dioo','Sort']];
foo[1] = [['What','Gary'],['Tofu',bili']];

Now it's more obvious that foo contains only two members. 现在,很明显foo仅包含两个成员。 It just so happens that those members are arrays. 碰巧那些成员是数组。

So foo.every() will iterate over foo[0] and foo[1] . 因此foo.every()将遍历foo[0]foo[1] If you need to iterate over the contents of foo then you need to call .every() on them as well like foo[0].every() 如果您需要遍历foo的内容,则需要像对foo[0].every()一样对它们调用.every()

您可能要三维地找到

var firstJOJO=foo.find(arr=>arr.find(inner=>inner.find(isJOJO)));

As the other answers describe, the top level elements of foo are arrays, so if you want to look at the values in them, you can use recursion: 正如其他答案所描述的那样, foo的顶级元素是数组,因此,如果要查看它们中的值,可以使用递归:

 var foo = []; foo[0] = []; foo[1] = []; foo[0][0] = []; foo[0][1] = []; foo[1][0] = []; foo[1][1] = []; foo[0][0][0] = "kanon"; foo[0][0][1] = "JOJO"; foo[0][1][0] = "Dio"; foo[0][1][1] = "Sort"; foo[1][0][0] = "What"; foo[1][0][1] = "Gary"; foo[1][1][0] = "Tofu"; foo[1][1][1] = "bili"; var foo2 = ["JOJO","JOJO","mon","ti","JOJO"]; function isJOJO(x){ if(Array.isArray(x)){ return x.every(isJOJO); }else if(x === "JOJO"){ console.log(x + " is JOJO."); return true; }else{ console.log(x + " is not JOJO."); return false; } } console.log(foo.every(isJOJO)); console.log(foo2.every(isJOJO)); 

Javascript does not do multi-dimensional arrays per se. Javascript本身并不做多维数组。 It does arrays of arrays to the nth degree. 它将数组的数组进行到第n次。 So in your case your foo is an array of arrays and each subarray is an array of arrays. 因此,在您的情况下,您的foo是一个数组数组,每个子数组都是一个数组数组。

The values that get passed to your isJOJO function when you call foo.every(isJOJO) are therefore an array of arrays. 因此,在调用foo.every(isJOJO)时传递给isJOJO函数的值是一个数组数组。 That is why it is not identical to "JOJO". 这就是为什么它与“ JOJO”不同的原因。

You will want to test if the value passed into the function is an array or not and if it is, to run: x.every(isJOJO). 您将要测试传递给函数的值是否是数组,如果要运行,请运行:x.every(isJOJO)。

The answer posted by Barmar explains in clear terms why the code doesn't work. Barmar发布答案清楚地解释了为什么代码不起作用。 Start there. 从那里开始。

To recursively find elements in your multi-dimensional array, you can write a deepEvery function: 要递归地在多维数组中查找元素,可以编写deepEvery函数:

function deepEvery(array, condition) {
  const results = []

  for (let item of array) {
    if (Array.isArray(item)) {
      // Handle internal arrays with a recursive call:
      for (let subitem of deepEvery(item, condition)) results.push(subitem)

    } else {
      // Handle single items by testing the condition:
      if (condition(item)) results.push(item)
    }
  }

  return results
}

For example: 例如:

const array = [
  [
    ["foo", "bar"]
  ],
  [ "foo" ],
  "foo"
]

console.log(deepEvery(array, x => x === 'foo'))
# ['foo', 'foo', 'foo']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM