将 for 循环变成 foreach 循环不起作用
Turn a for loop into a foreach loop not working
我正在尝试将 for
循环变成 forEach
循环,但它似乎不起作用...
这是我的代码:
const townDataURL = "[some link I probably can't disclose...]"
const towns2get = [
"Preston",
"Fish Haven",
"Soda Springs"
]
fetch(townDataURL)
.then((response) => {
return response.json()
})
.then((jsonData) => {
const towns = jsonData["towns"].filter((item) => {
// for (let i = 0; i<towns2get.length; i++) {
// if (item.name == towns2get[i]) {
// return item
// }
// }
return (towns2get.forEach(elem => {
return ( (item.name == elem) ? (item) : "Hello?" )
}))
})
console.log(towns)
})
当我有注释代码 运行 时,它给了我这个:
(3) [{…}, {…}, {…}]
0: {name: "Fish Haven", photo: "fishhaven.jpg", motto: "This is Fish Heaven.", yearFounded: 1864, currentPopulation: 501, …}
1: {name: "Preston", photo: "preston.jpg", motto: "Home of Napoleon Dynamite.", yearFounded: 1866, currentPopulation: 5204, …}
2: {name: "Soda Springs", photo: "sodasprings.jpg", motto: "Historic Oregon Trail Oasis. The Soda is on Us.", yearFounded: 1858, currentPopulation: 2985, …}
length: 3
__proto__: Array(0)
这正是我想要的,但我想简化我的代码...我现在拥有的是:
[]
length: 0
__proto__: Array(0)
我已经进行了一些调试,我知道我的条件语句与三元运算符工作正常,并且它正在返回一个值...但我似乎无法弄清楚为什么它没有返回它回到 filter
方法...
这样不行吗?或者我是否必须以某种方式将 forEach
与 filter
放在一起?
感谢您的帮助!
这里更好的方法是在过滤方法中使用 .includes 函数
const townDataURL = "[some link I probably can't disclose...]"
const towns2get = [
"Preston",
"Fish Haven",
"Soda Springs"
]
fetch(townDataURL)
.then((response) => {
return response.json()
})
.then((jsonData) => {
const towns = jsonData["towns"].filter((item) => {
if(towns2get.includes(item.name) > -1) return true;
else return false;
})
console.log(towns)
})
Cris G给了我答案:
.forEach()
doesn't return anything, so your .filter()
callback returns undefined
, a falsey value, for each element. Use filter(item => towns2get.includes(item.name))
– Chris G
所以代码应该是:
const townDataURL = "[some link I probably can't disclose...]"
const towns2get = [
"Preston",
"Fish Haven",
"Soda Springs"
]
fetch(townDataURL)
.then((response) => {
return response.json()
})
.then((jsonData) => {
const towns = jsonData["towns"].filter(item => towns2get.includes(item.name))
console.log(towns)
})
我正在尝试将 for
循环变成 forEach
循环,但它似乎不起作用...
这是我的代码:
const townDataURL = "[some link I probably can't disclose...]"
const towns2get = [
"Preston",
"Fish Haven",
"Soda Springs"
]
fetch(townDataURL)
.then((response) => {
return response.json()
})
.then((jsonData) => {
const towns = jsonData["towns"].filter((item) => {
// for (let i = 0; i<towns2get.length; i++) {
// if (item.name == towns2get[i]) {
// return item
// }
// }
return (towns2get.forEach(elem => {
return ( (item.name == elem) ? (item) : "Hello?" )
}))
})
console.log(towns)
})
当我有注释代码 运行 时,它给了我这个:
(3) [{…}, {…}, {…}]
0: {name: "Fish Haven", photo: "fishhaven.jpg", motto: "This is Fish Heaven.", yearFounded: 1864, currentPopulation: 501, …}
1: {name: "Preston", photo: "preston.jpg", motto: "Home of Napoleon Dynamite.", yearFounded: 1866, currentPopulation: 5204, …}
2: {name: "Soda Springs", photo: "sodasprings.jpg", motto: "Historic Oregon Trail Oasis. The Soda is on Us.", yearFounded: 1858, currentPopulation: 2985, …}
length: 3
__proto__: Array(0)
这正是我想要的,但我想简化我的代码...我现在拥有的是:
[]
length: 0
__proto__: Array(0)
我已经进行了一些调试,我知道我的条件语句与三元运算符工作正常,并且它正在返回一个值...但我似乎无法弄清楚为什么它没有返回它回到 filter
方法...
这样不行吗?或者我是否必须以某种方式将 forEach
与 filter
放在一起?
感谢您的帮助!
这里更好的方法是在过滤方法中使用 .includes 函数
const townDataURL = "[some link I probably can't disclose...]"
const towns2get = [
"Preston",
"Fish Haven",
"Soda Springs"
]
fetch(townDataURL)
.then((response) => {
return response.json()
})
.then((jsonData) => {
const towns = jsonData["towns"].filter((item) => {
if(towns2get.includes(item.name) > -1) return true;
else return false;
})
console.log(towns)
})
Cris G给了我答案:
.forEach()
doesn't return anything, so your.filter()
callback returnsundefined
, a falsey value, for each element. Usefilter(item => towns2get.includes(item.name))
– Chris G
所以代码应该是:
const townDataURL = "[some link I probably can't disclose...]"
const towns2get = [
"Preston",
"Fish Haven",
"Soda Springs"
]
fetch(townDataURL)
.then((response) => {
return response.json()
})
.then((jsonData) => {
const towns = jsonData["towns"].filter(item => towns2get.includes(item.name))
console.log(towns)
})