Javascript 将for循环转换为foreach循环不工作

Javascript 将for循环转换为foreach循环不工作,javascript,arrays,for-loop,foreach,conditional-operator,Javascript,Arrays,For Loop,Foreach,Conditional Operator,我试图将for循环转换为forEach循环,但它似乎不起作用 这是我的密码: const townDataURL=“[一些我可能无法透露的链接…” const towns2get=[ “普雷斯顿”, “鱼港”, “苏打泉” ] 获取(townDataURL) 。然后((响应)=>{ 返回response.json() }) .然后((jsonData)=>{ const towns=jsonData[“towns”]。过滤器((项目)=>{ //for(设i=0;i{ 返回((item.name

我试图将
for
循环转换为
forEach
循环,但它似乎不起作用

这是我的密码:

const townDataURL=“[一些我可能无法透露的链接…”
const towns2get=[
“普雷斯顿”,
“鱼港”,
“苏打泉”
]
获取(townDataURL)
。然后((响应)=>{
返回response.json()
})
.然后((jsonData)=>{
const towns=jsonData[“towns”]。过滤器((项目)=>{
//for(设i=0;i{
返回((item.name==elem)?(item):“您好?”)
}))
})
控制台日志(城镇)
})
当我运行注释代码时,它会给出以下信息:

(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)
这正是我想要的,但我想简化我的代码。。。我现在得到的是:

(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)
我已经做了一些调试,我知道我的带有三元运算符的条件语句工作得很好,它返回一个值。。。但我似乎不明白为什么它不将其返回到
过滤器
方法

这样不行吗?或者我是否必须以某种方式将
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)
    })

这里更好的方法是在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)
    })

克里斯·G给了我答案:

.forEach()
不会返回任何内容,因此您的
.filter()
回调函数会为每个元素返回
未定义的值。使用
过滤器(item=>towns2get.includes(item.name))
-

因此,代码应该是:

const townDataURL=“[一些我可能无法透露的链接…”
const towns2get=[
“普雷斯顿”,
“鱼港”,
“苏打泉”
]
获取(townDataURL)
。然后((响应)=>{
返回response.json()
})
.然后((jsonData)=>{
const towns=jsonData[“towns”].filter(item=>towns2get.includes(item.name))
控制台日志(城镇)
})

Cris G给了我答案:

.forEach()
不会返回任何内容,因此您的
.filter()
回调函数会为每个元素返回
未定义的值。使用
过滤器(item=>towns2get.includes(item.name))
-

因此,代码应该是:

const townDataURL=“[一些我可能无法透露的链接…”
const towns2get=[
“普雷斯顿”,
“鱼港”,
“苏打泉”
]
获取(townDataURL)
。然后((响应)=>{
返回response.json()
})
.然后((jsonData)=>{
const towns=jsonData[“towns”].filter(item=>towns2get.includes(item.name))
控制台日志(城镇)
})

返回
未定义的
——我将使用它来实现我认为您想要的…
。forEach()
不返回任何内容,因此您的
.filter()
回调为每个元素返回
未定义的
,一个错误值。使用
.filter(town=>towns2get.includes(town.name))
您可能需要在
地图中使用索引。。。这是您将提供的回调的第二个参数:
towns2get.map((item,index){…}
;)这是否回答了您的问题?不客气,但这仍然是一个骗局:(for循环业务是无关的;转入forEach循环不是实际问题的解决方案)返回
未定义的
——我将使用它来实现我认为您想要的…
。forEach()
不返回任何内容,因此您的
过滤器()
callback为每个元素返回
undefined
,一个假值。使用
.filter(town=>towns2get.includes(town.name))
您可能需要在
地图中使用索引。。。这是您将提供的回调的第二个参数:
towns2get.map((item,index){…}
;)这是否回答了您的问题?不客气,但它仍然是一个重复:(for循环业务是不相关的;转换为forEach循环并不是实际问题的解决方案)我认为OP不想过滤数组,而是想用其他东西替换不匹配的值(“示例中的Hello?”)我猜他是用它来暂时检查病情的…但这不是他预期的结果嗯。。。也许吧!;)@Louyspatricebesette-那只是我在测试它是否有任何作用。。。Amaarshall Yaswankar是对的,我不希望它出现在我的输出中。@Amaarshall Yaswankar-这个答案几乎是我从其他人那里得到的。。。虽然他们的内容稍微浓缩了一点。。。谢谢你的回答,虽然,它帮助我更好地理解它!我认为OP不想过滤数组,而是想用其他东西替换一个不匹配的值(“示例中的“你好”)我猜他是用它来临时检查条件的……但这不是他预期的输出mmm中的目的。。。也许吧!;)@Louyspatricebesette-那只是我在测试它是否有任何作用。。。Amaarshall Yaswankar是对的,我不希望它出现在我的输出中。@Amaarshall Yaswankar-这个答案几乎是我从其他人那里得到的。。。虽然他们的内容稍微浓缩了一点。。。谢谢你的回答,虽然,它帮助我更好地理解它!