Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a list element starting with a letter in JavaScript

I am trying to sort out list elements that starts with 'N' into a new list. Why won't this work?

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())
like image 554
Sander B Avatar asked Nov 06 '16 20:11

Sander B


1 Answers

const freinds = ['Salman', 'Asad',  'Shahrukh', 'Aman'];
const startsS = freinds.filter((freind) => freind.startsWith("s"));
console.log(startsS);

Output:

[ 'Salman', 'Shahrukh' ]

like image 85
sherkhan Avatar answered Nov 13 '22 05:11

sherkhan