我正在尝试将以“N”开头的列表元素整理到一个新列表中。
为什么这不起作用?
const countries = ['Norway', 'Sweden', 'Denmark', 'New Zealand'];
function firstN(){
for (let i=0;i<countries.length;i++){
countries[i].startsWith("N")
}
}
let startsWithN = countries.filter(firstN())
这与@adeneo的答案大致相同,只是更简洁一些:
const countries = ['Norway', 'Sweden', 'Denmark', 'New Zealand'];
const startsWithN = countries.filter((country) => country.startsWith("N"));
console.log(startsWithN);
// Output: [ 'Norway', 'New Zealand' ]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)