I've got the following array :
[David, Daniel, Dude, Dwayne]
How do I get a list of the names that starts with "Da"? The output should be :
[David, Daniel]
Thanks.
CodePudding user response:
You can use .filter()
as -
["David", "Daniel", "Dude", "Dwayne"].filter((element)=> element.startsWith('Da'))
Outputs
Array [ "David", "Daniel" ]
The filter function is applied on an array. Inside this, we are just checking if the element starts with Da
, if that condition returns false, the function will filter that element out.
More array methods you can find here