简体   繁体   English

使用 forEach 遍历数组

[英]Looping through array with forEach

I am trying to make a basic program that loops through an array and logs the 'place' & 'distance' to the console.我正在尝试制作一个循环遍历数组并将“位置”和“距离”记录到控制台的基本程序。 My program isn't working as intended and won't log the values to the console.我的程序没有按预期运行,不会将值记录到控制台。 What am I doing wrong and how can I fix it?我做错了什么,我该如何解决?

let destinations = [
  {place: ['Dubai'],
  distance: 7276},
  {place: ['Sydney'],
  distance: 8759},
  {place: ['London'],
  distance: 4166},
  {place: ['tokyo'],
  distance: 5754},
  {place: ['Seoul'],
  distance: 6037},
];

destinations.forEach(spot => {
    if (spot.length < 6) {
      console.log(`${spot} is an interesting vacation spot.
      Let's see how far it is before we pull the trigger`);
      destinations.forEach(howFar => {
        if (howFar < 6000) {
          console.log(`${spot} is close enough. Let's go there!`);
        } if (howFar > 6000) {
          console.log(`Nevermind. ${spot} is too far.`);
        }});
    }
});

You'll want to step through the code very carefully to try to see what the JS interpreter sees.您需要非常仔细地逐步执行代码,以尝试查看 JS 解释器看到的内容。 It can help to put down the value of variables on scratch paper and how they change as you loop.在草稿纸上记下变量的值以及它们在循环时如何变化会有所帮助。 That'll help you see your own mistakes:这将帮助您看到自己的错误:

destinations.forEach(spot => {

What is spot here?什么是spot It'll be each value of destinations , so for example on the first iteration it'll be {place: ['Dubai'],distance: 7276} .它将是destinations的每个值,因此例如在第一次迭代中它将是{place: ['Dubai'],distance: 7276}

if (spot.length < 6) {

What would the length property be for {place: ['Dubai'],distance: 7276} ? {place: ['Dubai'],distance: 7276}length属性是多少? That doesn't look like it'd have a length property.这看起来不像它有length属性。 It's not an Array.它不是一个数组。 Plus, what condition are you checking here?另外,你在这里检查什么条件? Are you sure you need an if statement here?你确定你需要一个 if 语句吗?

console.log(`${spot} is an interesting vacation spot.
Let's see how far it is before we pull the trigger`);

What would you expect to see putting spot in a string here?您希望看到什么在这里将spot放在字符串中? Its value is {place: ['Dubai'],distance: 7276} .它的值为{place: ['Dubai'],distance: 7276} Are you sure you're not wanting to get some value from the object to put into the string?您确定不想从 object 中获取一些值以放入字符串中吗?

destinations.forEach(howFar => {

Isn't this looping over the same Array you looped over earlier?这不是在你之前循环过的同一个数组上循环吗? Given 6 destinations, this means it'll run 36 times.给定 6 个目的地,这意味着它将运行 36 次。 And what is howFar ? howFar是什么? Since it's from destinations.forEach , it'll be one of the objects in the destinations array.因为它来自destinations.forEach ,所以它将是destinations数组中的对象之一。

if (howFar < 6000) {

Would an object in the destinations array be comparable to a number? destinations数组中的 object 是否与数字相当? It's an object, so that won't work.它是一个 object,所以这是行不通的。 Did you mean to access the distance property of the object?您是要访问 object 的distance属性吗?

My first piece of advice is to simplify your destinations Array.我的第一条建议是简化您的destinations数组。 place is an Array of strings currently, but they're all single items, so you probably don't need an Array for that. place目前是一个字符串数组,但它们都是单个项目,因此您可能不需要一个数组。 Also, since you're not changing destinations , you could make it aa const variable which is helpful because it lets someone looking at your code know they don't have to worry about finding places where it might be changing:此外,由于您没有更改destinations ,您可以将它设为一个const变量,这很有用,因为它可以让查看您的代码的人知道他们不必担心找到可能发生变化的地方:

const destinations = [
  {place: 'Dubai', distance: 7276},
  {place: 'Sydney', distance: 8759},
  {place: 'London', distance: 4166},
  {place: 'Tokyo', distance: 5754},
  {place: 'Seoul', distance: 6037},
];

Your way of looping through the destinations is fine, but you'll need to access the properties of the object, and you don't need the extraneous inner loop and if statement (unless you meant to do something with it that I'm not understanding).您循环遍历目的地的方式很好,但是您需要访问 object 的属性,并且您不需要无关的内部循环和 if 语句(除非您打算用它做一些我不想做的事情理解)。

destinations.forEach(spot => {
  console.log(`${spot.place} is an interesting vacation spot.
    Let's see how far it is before we pull the trigger`);
  if (spot.distance < 6000) {
    console.log(`${spot.place} is close enough. Let's go there!`);
  } else {
    console.log(`Nevermind. ${spot.place} is too far.`);
  }
});

You are basically doing an Object.length, wich doesn't exist.你基本上是在做一个 Object.length,它不存在。 Try something like spot.place or spot.distance.试试 spot.place 或 spot.distance 之类的东西。

Also, you cannot do a destinations.forEach inside destinations.此外,您不能在目的地内执行 destinations.forEach。 Is this really necessary?这真的有必要吗? Try something like this:尝试这样的事情:

destinations.forEach(spot => {
if (spot.place.length < 6) {
  console.log(`${spot.place} is an interesting vacation spot.
  Let's see how far it is before we pull the trigger`);
  if (spot.distance < 6000) {
     console.log(`${spot.place} is close enough. Let's go there!`);
  } else {
      console.log(`Nevermind. ${spot.place} is too far.`);
  }
}})

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

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