DEV Community

loading...
Cover image for Arrays with a Splash of Filter & Object String Prototype..

Arrays with a Splash of Filter & Object String Prototype..

xxbricksquadxx profile image Colby Cardell ・1 min read

So lets assume we know what an Array is already and jump into Filter & String Object!


// TODO: 'filter' out students whose name begins with "A"

const listOfStudents = 

['Adam','Goose','Steve','Ace','Maverick','Aaron']

Enter fullscreen mode Exit fullscreen mode

const aNameStudents = listOfStudents.filter(function(student) {
return student.startsWith('A')
})

Enter fullscreen mode Exit fullscreen mode

In the example above 🔼 Filter returns New Array with elements that meet some Boolean condition.👆 Filter returns the current element 'student' if our Callback returns true ✅ Soo we know from my last post that JS distinguishes between 'Primitive Strings' & now we have heard of 'String Objects'..Im not going to give the MDN definition in this post 🦾 But Primitive values like 'num', cant have properties or methods(because they are not objects) under the hood JS, treats primitive values as objects when executing methods and properties... 'startsWith' 🔼 is one of those invoked string methods that is going to return with a true or false if string starts with 'A'..

Discussion (0)

pic
Editor guide