admin管理员组文章数量:1326515
I'm learning JavaScript for the first time and I would like to know why my code doesn't work. I have Python/Django knowledges.
Objective :
I have to create a list of names and I have to display only firstnames which begins by 'B' letter.
My script :
var listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
for (var i in listNames) {
if (i.substr(0, 1) === 'B') {
console.log(i);
}
}
But this code doesn't display something.
I'm learning JavaScript for the first time and I would like to know why my code doesn't work. I have Python/Django knowledges.
Objective :
I have to create a list of names and I have to display only firstnames which begins by 'B' letter.
My script :
var listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
for (var i in listNames) {
if (i.substr(0, 1) === 'B') {
console.log(i);
}
}
But this code doesn't display something.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 29, 2018 at 12:12 EssexEssex 6,13811 gold badges77 silver badges142 bronze badges 2-
6
use
for ... of
instead offor ... in
. – ASDFGerte Commented Jun 29, 2018 at 12:13 -
Oh thank you ! I have to see the difference between
for ... of
andfor .. in
– Essex Commented Jun 29, 2018 at 12:14
5 Answers
Reset to default 8You need to use listNames[i]
as i
gives you the index
of array listNames
. Then use charAt(0)
to the value of the array to check for the first character as B
.
var listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
for (var i in listNames) {
if (listNames[i].charAt(0) === 'B') {
console.log(listNames[i]);
}
}
If you want to use the values of array that start with B
further in your code as a separate array then use filter()
:
var listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
var res = listNames.filter(item => item.charAt(0) === 'B');
console.log(res);
You should use forEach
, not for...in
as it's for iterate objects
And the i.substr(0, 1) === 'B'
could be replaced with i.startsWith('B')
var listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
listNames.forEach( i => {
if (i.startsWith('B')) {
console.log(i);
}
})
Or for...of
var listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
for (i of listNames) {
if (i.startsWith('B')) {
console.log(i);
}
}
Yet another option could be to use filter()
and reduce the original array into a new.
var listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
var new_listNames = listNames.filter( i => i.startsWith('B') )
console.log(new_listNames);
Try this:
listNames.forEach((el) => {if (el.charAt(0) == 'B') console.log(el)});
Use the for...of loop to loop over arrays.
const listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
for (const name of listNames) {
if (name[0] === "B") {
console.log(name);
}
}
Try this
var listNames = ['Paul', 'Bruno', 'Arthur', 'Bert', 'José']
for (var i of listNames) {
if (i.substr(0, 1) === 'B') {
console.log(i);
}
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
本文标签: Learning JavaScriptdisplay all firstnames with B as first letterStack Overflow
版权声明:本文标题:Learning JavaScript : display all firstnames with B as first letter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742199572a2431704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论