简体   繁体   English

数组推调用后在方括号中声明的目的是什么?

[英]What is the purpose of a statement in square brackets after an array push call?

I've been working at a problem on FreeCodeCamp called "9 billion names of God the integer" . 我一直在FreeCodeCamp上研究一个名为“ 90亿个整数上帝的名字”的问题 (The specifics of the problem itself aren't germane to my question, but have a look at the link if you're interested.) Admittedly I wrestled with this problem for several days before giving up and googling the answer. (问题的具体内容与我的问题无关,但是,如果您有兴趣,请查看链接。)诚然,我在放弃和谷歌搜索答案之前已经花了几天时间来解决这个问题。 The problem originally comes from Rosetta Code, and I set about reading the answer provided for JavaScript to make sure I understood how this problem could be solved. 问题最初来自Rosetta Code,我着手阅读为JavaScript提供的答案,以确保我了解如何解决此问题。

As far as I can tell, it's making calculations via good old-fashioned for loops, and even though it was published with short, non-descriptive variable names, I think I could work my way through it. 据我所知,它是通过老式的for循环进行计算的,即使它发布时带有简短的,非描述性的变量名,我想我也可以自己解决。 However, here is the part that's tripping me up (note that the code below is a cleaned-up reproduction of the Rosetta Code solution, which had a lot of unnecessary comments and a couple of typos): 但是,这是让我感到困惑的部分(请注意,下面的代码是Rosetta Code解决方案的清理后的复制品,其中包含很多不必要的注释和错别字):

(function() {
  var cache = [
    [1]
  ];

  function cumu(n) {
    var r, l, x, Aa, Mi;
    for (l = cache.length; l < n + 1; l++) {
      r = [0];
      for (x = 1; x < l + 1; x++) {
        r.push(r[r.length - 1] + (Aa = cache[l - x < 0 ? cache.length - (l - x) : l - x])[(Mi = Math.min(x, l - x)) < 0 ? Aa.length - Mi : Mi]);
      }
      cache.push(r);
    }
    return cache[n];
  }

  function row(n) {
    var r = cumu(n),
      leArray = [],
      i;
    for (i = 0; i < n; i++) {
      leArray.push(r[i + 1] - r[i]);
    }
    return leArray;
  }

  console.log("Rows:");
  for (iterator = 1; iterator < 12; iterator++) {
    console.log(row(iterator));
  }

  console.log("Sums");
  [23, 123, 1234].forEach(function(a) {
    var s = cumu(a);
    console.log(a, s[s.length - 1]);
  });
})()

Specifically this line within cumu(n) : 特别是在cumu(n)这一行:

r.push(r[r.length - 1] + (Aa = cache[l - x < 0 ? cache.length - (l - x) : l - x])[(Mi = Math.min(x, l - x)) < 0 ? Aa.length - Mi : Mi]);

This push method has [square brackets] after it. push方法后面有[square brackets] What does that do? 那是做什么的? I know the purpose of brackets as they pertain to arrays and objects, but I can't find any documentation about this usage. 我知道括号的用途,因为它们与数组和对象有关,但是找不到有关此用法的任何文档。 Mind you, the script seems to work as intended regardless, with the result printing to the console as expected and no errors coming up. 请注意,脚本似乎可以按预期运行,结果按预期输出到控制台,并且不会出现错误。

(Aa = cache[l - x < 0 ? cache.length - (l - x) : l - x]) 

returns an array as cache is a multidimensional array. 返回数组,因为cache是多维数组。 It is between the parentheses because Aa needs to be set as well. 它位于括号之间,因为还需要设置Aa

The great problem of this code is that it is hard to read. 此代码的主要问题是很难阅读。 It could be wise to open up your IDE and restructure the code piece by piece. 打开您的IDE并逐段重组代码可能是明智的。 Then you will be able to better understand it. 这样您将能够更好地理解它。

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

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