在Javascript中循环数组[重复]

时间:2021-03-20 22:56:38

This question already has an answer here:

这个问题在这里已有答案:

I'm using Google Chrome's Console window to try and figure out why I'm not able to loop over an array in javascript.

我正在使用Google Chrome的控制台窗口来尝试找出为什么我无法在javascript中循环遍历数组。

I have a javascript object called moveResult that looks like this:

我有一个名为moveResult的javascript对象,如下所示:

在Javascript中循环数组[重复]

I'm trying to loop over the MoveParts in javascript like this:

我试图在这样的javascript中循环移动MoveParts:

for (var movePart in moveResult.MoveParts) {
    console.log(movePart.From);
};

I always get undefined instead of the actual value. However, If I try to access the first item explicitly I get what I want, like this:

我总是得到undefined而不是实际值。但是,如果我尝试显式访问第一项,我会得到我想要的,如下所示:

console.log(moveResult.MoveParts[0].From);

The result of this is "b1".

结果是“b1”。

Why isn't my loop working?

为什么我的循环不起作用?

I've also tried a foreach:

我也试过一个foreach:

moveResult.MoveParts.foreach(function (movePart) {
    console.log(movePart.From);
};

2 个解决方案

#1


7  

I'm trying to loop over the MoveParts in javascript like this:

我试图在这样的javascript中循环移动MoveParts:

for (var movePart in moveResult.MoveParts) {
    console.log(movePart.From);
};

I always get undefined instead of the actual value.

我总是得到undefined而不是实际值。

Don't use for-in to loop through arrays, that's not what it's for. for-in is for looping through object properties. This answer shows various ways to loop through arrays.

不要使用for-in循环遍历数组,这不是它的用途。 for-in用于循环对象属性。这个答案显示了循环数组的各种方法。

The reason your for-in didn't work is that movePart is the key, not the actual entry, so if you were using an object (not an array!) you would have used moveResult.MoveParts[movePart].From.

你的for-in不起作用的原因是movePart是关键,而不是实际的条目,所以如果你使用的是一个对象(不是一个数组!),你会使用moveResult.MoveParts [movePart]。

Your forEach version only failed because:

您的forEach版本仅失败,原因是:

  1. It's forEach, not foreach. Capitalization matters in JavaScript.

    这是每次,而不是预告。资本化在JavaScript中很重要。

  2. You were missing the closing ) on the function call.

    你错过了函数调用的结束。

The answer linked above has full examples of forEach and others, but here's how yours should have looked:

上面链接的答案有forEach和其他的完整示例,但这是你的看法:

    moveResult.MoveParts.forEach(function (movePart) {
    // Capital E -----------^
        console.log(movePart.From);
    });
//   ^---- closing )

#2


-2  

try

尝试

moveResult.MoveParts.map(function (movePart) {
    console.log(movePart.From);
};

#1


7  

I'm trying to loop over the MoveParts in javascript like this:

我试图在这样的javascript中循环移动MoveParts:

for (var movePart in moveResult.MoveParts) {
    console.log(movePart.From);
};

I always get undefined instead of the actual value.

我总是得到undefined而不是实际值。

Don't use for-in to loop through arrays, that's not what it's for. for-in is for looping through object properties. This answer shows various ways to loop through arrays.

不要使用for-in循环遍历数组,这不是它的用途。 for-in用于循环对象属性。这个答案显示了循环数组的各种方法。

The reason your for-in didn't work is that movePart is the key, not the actual entry, so if you were using an object (not an array!) you would have used moveResult.MoveParts[movePart].From.

你的for-in不起作用的原因是movePart是关键,而不是实际的条目,所以如果你使用的是一个对象(不是一个数组!),你会使用moveResult.MoveParts [movePart]。

Your forEach version only failed because:

您的forEach版本仅失败,原因是:

  1. It's forEach, not foreach. Capitalization matters in JavaScript.

    这是每次,而不是预告。资本化在JavaScript中很重要。

  2. You were missing the closing ) on the function call.

    你错过了函数调用的结束。

The answer linked above has full examples of forEach and others, but here's how yours should have looked:

上面链接的答案有forEach和其他的完整示例,但这是你的看法:

    moveResult.MoveParts.forEach(function (movePart) {
    // Capital E -----------^
        console.log(movePart.From);
    });
//   ^---- closing )

#2


-2  

try

尝试

moveResult.MoveParts.map(function (movePart) {
    console.log(movePart.From);
};