使用 forEach 遍历数组
Looping through array with forEach
我正在尝试制作一个循环遍历数组并将 'place' 和 'distance' 记录到控制台的基本程序。我的程序没有按预期运行,不会将值记录到控制台。我做错了什么,我该如何解决?
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.`);
}});
}
});
您需要非常仔细地单步执行代码以尝试查看 JS 解释器看到的内容。在草稿纸上记下变量的值以及它们在循环时如何变化会有所帮助。这将帮助您看到自己的错误:
destinations.forEach(spot => {
这里的spot
是什么?它将是 destinations
的每个值,因此例如在第一次迭代中它将是 {place: ['Dubai'],distance: 7276}
.
if (spot.length < 6) {
length
属性 对 {place: ['Dubai'],distance: 7276}
来说是什么?这看起来不像是 length
属性。它不是一个数组。另外,你在这里检查什么条件?您确定此处需要 if 语句吗?
console.log(`${spot} is an interesting vacation spot.
Let's see how far it is before we pull the trigger`);
在这里将 spot
放入字符串中,您希望看到什么?它的值为{place: ['Dubai'],distance: 7276}
。您确定不想从对象中获取一些值以放入字符串中吗?
destinations.forEach(howFar => {
这不是在循环你之前循环的同一个数组吗?给定 6 个目的地,这意味着它将 运行 36 次。什么是 howFar
?由于它来自 destinations.forEach
,它将成为 destinations
数组中的对象之一。
if (howFar < 6000) {
destinations
数组中的对象是否可以与数字进行比较?它是一个对象,所以这是行不通的。您是要访问对象的 distance
属性 吗?
我的第一条建议是简化您的 destinations
数组。 place
目前是一个字符串数组,但它们都是单个项目,因此您可能不需要数组。此外,由于您没有更改 destinations
,您可以将其设为一个 const
变量,这很有用,因为它可以让查看您的代码的人知道他们不必担心找到它可能存在的地方正在改变:
const destinations = [
{place: 'Dubai', distance: 7276},
{place: 'Sydney', distance: 8759},
{place: 'London', distance: 4166},
{place: 'Tokyo', distance: 5754},
{place: 'Seoul', distance: 6037},
];
您循环遍历目标的方式很好,但是您需要访问对象的 属性 ,并且不需要无关的内部循环,如果声明(除非你打算用它做一些我不理解的事情)。
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.`);
}
});
你基本上是在做一个 Object.length,这是不存在的。尝试 spot.place 或 spot.distance.
此外,您不能在目的地内部执行 destinations.forEach。这真的有必要吗?
尝试这样的事情:
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.`);
}
}})
我正在尝试制作一个循环遍历数组并将 'place' 和 'distance' 记录到控制台的基本程序。我的程序没有按预期运行,不会将值记录到控制台。我做错了什么,我该如何解决?
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.`);
}});
}
});
您需要非常仔细地单步执行代码以尝试查看 JS 解释器看到的内容。在草稿纸上记下变量的值以及它们在循环时如何变化会有所帮助。这将帮助您看到自己的错误:
destinations.forEach(spot => {
这里的spot
是什么?它将是 destinations
的每个值,因此例如在第一次迭代中它将是 {place: ['Dubai'],distance: 7276}
.
if (spot.length < 6) {
length
属性 对 {place: ['Dubai'],distance: 7276}
来说是什么?这看起来不像是 length
属性。它不是一个数组。另外,你在这里检查什么条件?您确定此处需要 if 语句吗?
console.log(`${spot} is an interesting vacation spot.
Let's see how far it is before we pull the trigger`);
在这里将 spot
放入字符串中,您希望看到什么?它的值为{place: ['Dubai'],distance: 7276}
。您确定不想从对象中获取一些值以放入字符串中吗?
destinations.forEach(howFar => {
这不是在循环你之前循环的同一个数组吗?给定 6 个目的地,这意味着它将 运行 36 次。什么是 howFar
?由于它来自 destinations.forEach
,它将成为 destinations
数组中的对象之一。
if (howFar < 6000) {
destinations
数组中的对象是否可以与数字进行比较?它是一个对象,所以这是行不通的。您是要访问对象的 distance
属性 吗?
我的第一条建议是简化您的 destinations
数组。 place
目前是一个字符串数组,但它们都是单个项目,因此您可能不需要数组。此外,由于您没有更改 destinations
,您可以将其设为一个 const
变量,这很有用,因为它可以让查看您的代码的人知道他们不必担心找到它可能存在的地方正在改变:
const destinations = [
{place: 'Dubai', distance: 7276},
{place: 'Sydney', distance: 8759},
{place: 'London', distance: 4166},
{place: 'Tokyo', distance: 5754},
{place: 'Seoul', distance: 6037},
];
您循环遍历目标的方式很好,但是您需要访问对象的 属性 ,并且不需要无关的内部循环,如果声明(除非你打算用它做一些我不理解的事情)。
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.`);
}
});
你基本上是在做一个 Object.length,这是不存在的。尝试 spot.place 或 spot.distance.
此外,您不能在目的地内部执行 destinations.forEach。这真的有必要吗? 尝试这样的事情:
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.`);
}
}})