最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Learning JavaScript : display all firstnames with B as first letter - Stack Overflow

programmeradmin7浏览0评论

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 of for ... in. – ASDFGerte Commented Jun 29, 2018 at 12:13
  • Oh thank you ! I have to see the difference between for ... of and for .. in – Essex Commented Jun 29, 2018 at 12:14
Add a ment  | 

5 Answers 5

Reset to default 8

You 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>

发布评论

评论列表(0)

  1. 暂无评论