简体   繁体   中英

How can I get the first letters of each word in JavaScript?

I need to get the first letter of each word in an array and log these first letters to the console.

These are the words:

var names = ["Vincent", "Charlotte", "Nandi", "Eiske", "Joachim", "Angelo", "Paul", "Chantal", "Olaf", "Inge", "Rogier", "Michael", "Ramon", "Carolien", "Johan", "Bianca", "Rene", "Yulia", "Bram", "Anneloes", "Kirsten", "Roel", "Gökhan", "Annemiek", "Lisette", "Menno", "Rene", "Erik", "Robin", "Frank", "Anton", "Maks", "Rob", "Floor", "Bas", "Rico", "Max", "Bastiaan", "Eugune"];


function getNames(names) {
    // 
}

You can use charAt(0) for each word in array. You can use simple forEach() loop for that:

 var names = ["Vincent", "Charlotte", "Nandi", "Eiske", "Joachim", "Angelo", "Paul", "Chantal", "Olaf", "Inge", "Rogier", "Michael", "Ramon", "Carolien", "Johan", "Bianca", "Rene", "Yulia", "Bram", "Anneloes", "Kirsten", "Roel", "Gökhan", "Annemiek", "Lisette", "Menno", "Rene", "Erik", "Robin", "Frank", "Anton", "Maks", "Rob", "Floor", "Bas", "Rico", "Max", "Bastiaan", "Eugune"]; function getNames(names){ names.forEach(item => console.log(item.charAt(0))); } getNames(names); 

You can also go with traditional way of for loop if you are new in using loops:

 var names = ["Vincent", "Charlotte", "Nandi", "Eiske", "Joachim", "Angelo", "Paul", "Chantal", "Olaf", "Inge", "Rogier", "Michael", "Ramon", "Carolien", "Johan", "Bianca", "Rene", "Yulia", "Bram", "Anneloes", "Kirsten", "Roel", "Gökhan", "Annemiek", "Lisette", "Menno", "Rene", "Erik", "Robin", "Frank", "Anton", "Maks", "Rob", "Floor", "Bas", "Rico", "Max", "Bastiaan", "Eugune"]; function getNames(names){ for(var i=0; i<names.length; i++){ console.log(names[i].charAt(0)) } } getNames(names); 

Also note that the function name and variable name in your case cannot be same which you are using as names as it will conflict.

You could map the first letter by using a destructuring assignment for the first letter.

 var names = ["Vincent", "Charlotte", "Nandi", "Eiske", "Joachim", "Angelo", "Paul", "Chantal", "Olaf", "Inge", "Rogier", "Michael", "Ramon", "Carolien", "Johan", "Bianca", "Rene", "Yulia", "Bram", "Anneloes", "Kirsten", "Roel", "Gökhan", "Annemiek", "Lisette", "Menno", "Rene", "Erik", "Robin", "Frank", "Anton", "Maks", "Rob", "Floor", "Bas", "Rico", "Max", "Bastiaan", "Eugune"], result = names.map(([c]) => c); console.log(result); 

Use array map:

 var names = ["Vincent", "Charlotte", "Nandi", "Eiske", "Joachim", "Angelo", "Paul", "Chantal", "Olaf", "Inge", "Rogier", "Michael", "Ramon", "Carolien", "Johan", "Bianca", "Rene", "Yulia", "Bram", "Anneloes", "Kirsten", "Roel", "Gökhan", "Annemiek", "Lisette", "Menno", "Rene", "Erik", "Robin", "Frank", "Anton", "Maks", "Rob", "Floor", "Bas", "Rico", "Max", "Bastiaan", "Eugune"]; var result = names.map(current=>{ return current[0]; }); console.log(result); 

 var names = ["Vincent", "Charlotte", "Nandi", "Eiske", "Joachim", "Angelo", "Paul", "Chantal", "Olaf", "Inge", "Rogier", "Michael", "Ramon", "Carolien", "Johan", "Bianca", "Rene", "Yulia", "Bram", "Anneloes", "Kirsten", "Roel", "Gökhan", "Annemiek", "Lisette", "Menno", "Rene", "Erik", "Robin", "Frank", "Anton", "Maks", "Rob", "Floor", "Bas", "Rico", "Max", "Bastiaan", "Eugune"], result = names.map(name => name[0]); console.log(result); 

const names = arr => arr.forEach(s => console.log(s[0]));

You can filter through the array using a for loop, and then push the first letter to a filtered array and then return that.

var names = ["Vincent", "Charlotte", "Nandi", "Eiske", "Joachim", "Angelo", "Paul", "Chantal", "Olaf", "Inge", "Rogier", "Michael", "Ramon", "Carolien", "Johan", "Bianca", "Rene", "Yulia", "Bram", "Anneloes", "Kirsten", "Roel", "Gökhan", "Annemiek", "Lisette", "Menno", "Rene", "Erik", "Robin", "Frank", "Anton", "Maks", "Rob", "Floor", "Bas", "Rico", "Max", "Bastiaan", "Eugune"]; // Names array


function checkNames(names) {
var f = [];
    for(var i = 0; i < names.length; i++){
    f.push(substring(names[i],0,1));
    }
return f;
}

var result_array = checkNames(names); // Filtered Array that will hold your single letters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM