【问题标题】:JavaScript loop push to new arrayJavaScript循环推送到新数组
【发布时间】:2016-05-18 20:07:17
【问题描述】:

我正在尝试将所有可被 3 整除的数字推入一个新的数组“threes”中,但由于此代码无法正常工作,我遇到了困难。

var numbers = function () {
  var threes =[]
  for (i = 0; i < numbers.length; i++) {
    if (iLoveThree[i] % 3 === 0){
      threes.push(numbers[i])
    }
  }
  return threes
}

【问题讨论】:

  • 这些
    标签是您代码的一部分还是格式问题?
  • 你的数字数组在哪里?
  • 看起来他们将numbers 定义为一个函数,并将其视为该函数中的一个数组。
  • 应该说:for (i = 0; i &lt; iLoveThree.length; i++) 而不是numbers.length?。只是猜测是因为if (iLoveThree[i] % 3 === 0)
  • 看起来像一个简单的错误。您将i 绑定到numbers.length,但您正在查看数组iLoveThree,而不是numbers'。

标签: javascript arrays for-loop methods push


【解决方案1】:

我认为对数字使用过滤器会更简单

var threes = numbers.filter(function(number) {
  return number % 3 === 0;
});

【讨论】:

  • 回滚我的编辑,以防原型编辑困扰一些开发人员(老实说,这并不是一个好主意),这里是解释代码的 JSFiddle jsfiddle.net/ekkk4e53
  • 这也行,我总是害怕扩展原生对象原型 doe
【解决方案2】:

我已经解决了你的问题,创建一个新的 html 文件并输入:

<!doctype html>
<HTML>

    <BODY>
        <SCRIPT>
            (function(){
                var numbers = function (iLoveThree) {
                    var threes =[];
                    for (i = 0; i < iLoveThree.length; i++) {
                        if (iLoveThree[i] % 3 === 0){
                            threes.push(iLoveThree[i]);
                        }
                      }
                    return threes;
                }
                alert(numbers([1, 2, 3, 4, 5, 6]));
            })();
        </SCRIPT>
    </BODY>
</HTML>

希望对你有帮助:)
说明:
- 需要包含函数参数,这个参数会在函数内部被访问(参数名为iLoveThree)
- 您使用的是 numbers 变量,但该变量之前未声明过,我通过将 numbers 更改为 iLoveThree 来解决此问题
- 你错过了几个; (分号),虽然简单但是会给你带来很多麻烦

PS:感谢RobG提醒我解释一下。

【讨论】:

  • 谢谢!让我绊倒的是我总是忽略的小事。
【解决方案3】:

你的例子有几个问题:

  • 您已将函数命名为“numbers”,但随后还在函数内部引用了一个名为“numbers”的不存在数组
  • iLoveThree 被作为数组引用,但从未被声明

根据您的应用程序的需要,您可能需要获取介于最小值和最大值之间的所有可被三整除的数字,或者您可能需要从预定义的数组中提取可被三整除的数字。我在下面的代码中包含了这两种情况的示例:

var isDivisibleByThree = function(num){
  return i % 3 === 0;
}

var getThrees = function (min, max) {
  // given a numeric min and max value,
  // return all numbers from a minimum to a maximum value that are divisible by three
  var threes =[];
  for (i = min; i <= max; i++) {
    if (isDivisibleByThree(i)){
      threes.push(i);
    }
  }
  return threes;
}

var getThreesFromArray = function(numbers){
  // given an array of numbers, 
  // return a subset of that array including  only numbers divisible by 3
  var threes = [];
  for (i = 0; i < numbers.length; i++) {
    if (isDivisibleByThree(numbers[i])){
      threes.push(i);
    }
  }
  return threes;
                 
}
var fromZeroTo50 = getThrees(0, 50);
var fromArray = getThreesFromArray([5, 0, 6, 17, 12, 4, 18]);

// Grab example values and display them in the HTML for demonstration purposes
document.getElementById("fromZeroTo50").innerHTML = fromZeroTo50.join(",");
document.getElementById("fromArray").innerHTML = fromArray.join(",");
<h2>Get all threes from 0 to 50: </h2>
<div id="fromZeroTo50"></div>

<h2>Get threes from a pre-defined array: </h2>
<div id="fromArray"></div>

【讨论】:

  • @RobG- 你是绝对正确的:) 用 vanilla JS 更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多