通过阵列循环时,我希望将元素推向新数组。但是数组保持空

发布于 2025-01-30 17:34:09 字数 327 浏览 8 评论 0原文

当我运行此循环时,它将带入一个包含八个数字的数组,它循环循环,然后将它们推到命名结果的全局数组中。由于某种原因,当我委托时。我希望它能从另一个数组返回八个数字。

const results = []

const validate = arr => {
  for(let i = 0; i < arr.length; i++){
    results.push(arr[i])
  }

}
console.log(results)

When I run this loop it takes in an array which contains eight numbers, it loops through and simply pushes them to a global array which is named results. For some reason when I console.log this array outside of the loop it returns 0 []. I wish it to return the eight numbers from the other array.

const results = []

const validate = arr => {
  for(let i = 0; i < arr.length; i++){
    results.push(arr[i])
  }

}
console.log(results)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清音悠歌 2025-02-06 17:34:09

您在这里所做的只是定义一个函数,该函数复制已传递到其中的数组的内容。您需要调用功能
验证具有适当的值。

const results = []

const validate = arr => {
  for(let i = 0; i < arr.length; i++){
    results.push(arr[i])
  }

}
validate([1,2,3,4,5,6,7,8]);
console.log(results);

What you have done here is simply define a function that copies the contents of the array that has been passed into it. You need to call the function
validate with the appropriate value.

const results = []

const validate = arr => {
  for(let i = 0; i < arr.length; i++){
    results.push(arr[i])
  }

}
validate([1,2,3,4,5,6,7,8]);
console.log(results);

猫九 2025-02-06 17:34:09

那是因为您必须运行函数验证,现在您只是打印result []没有项目。

validate(arr);

const results = []
const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const validate = arr => {
  for (let i = 0; i < arr.length; i++) {
    results.push(arr[i])
  }

}
validate(arr);
console.log(results);

但是,如果您想制作副本,则可以使用:

const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const results = [...arr];
console.log(results);

That is because you have to run the function validate, now you are just printing results[] which has no items.

validate(arr);

const results = []
const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const validate = arr => {
  for (let i = 0; i < arr.length; i++) {
    results.push(arr[i])
  }

}
validate(arr);
console.log(results);

But if you want to make a copy, you can just use:

const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const results = [...arr];
console.log(results);

述情 2025-02-06 17:34:09

您错过了调用功能。

const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const result = [];
arr.map((num) => result.push(num));
console.log(result);

You missed calling the function.

const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const result = [];
arr.map((num) => result.push(num));
console.log(result);

黒涩兲箜 2025-02-06 17:34:09
const results = []
const validate = (arr) => {
    console.log(arr)
    arr.forEach((value)=>{
        results.push(value)
    });
}

validate([1,2,3,4,5,6,7,8]);
console.log(results);
const results = []
const validate = (arr) => {
    console.log(arr)
    arr.forEach((value)=>{
        results.push(value)
    });
}

validate([1,2,3,4,5,6,7,8]);
console.log(results);
┊风居住的梦幻卍 2025-02-06 17:34:09

如果您包含了数字的原始数组,则您的代码将起作用,并调用迭代的函数:

let arr = [1,2,3,4,5,6,7,8];
let results = [];

function validate() {
  for(let i = 0; i < arr.length; i++){
    results.push(arr[i])
  }
}

validate();
console.log(results)

但是, map() 方法旨在做这种类型的事情:

let arr = [2,4,6,8];

let results = arr.map(function(element){
  return element; // Returns item to resulting array
});

console.log(results)

Your code would work if you included the original array of numbers and you call the function that iterates over it:

let arr = [1,2,3,4,5,6,7,8];
let results = [];

function validate() {
  for(let i = 0; i < arr.length; i++){
    results.push(arr[i])
  }
}

validate();
console.log(results)

But, the Array.prototype.map() method was designed to do just this type of thing:

let arr = [2,4,6,8];

let results = arr.map(function(element){
  return element; // Returns item to resulting array
});

console.log(results)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文