DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 968,873 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Aboucodeur
Aboucodeur

Posted on

JavaScript Filter

// Filter in Javascript

// Datas
const userDatas = [
  {firstName : "Aboubacar" , lastName : "Barry"},
  {firstName : "Bill" , lastName : "Gates"},
  {firstName : "Amadou" , lastName : "DiakitΓ©"}
]

// filter function
const filteredDatas = (datas,props,str)=>{
  if(!props || props === null){
    return null;
  }
    const filter = datas.filter(data=>{
      if(data.[props].startsWith(str)){
        return data
      }
    })
//     global return 
    return (filter.length === 0 ? "Not Founds In Tables" : filter); 
}

// results
filteredDatas(userDatas,"firstName","A") 
// [ {firstName : "Aboubacar" , lastName : "Barry"}, {firstName : "Amadou" , lastName : "DiakitΓ©"}]

filteredDatas(userDatas,"","M") 
// null
Enter fullscreen mode Exit fullscreen mode

Latest comments (1)

Collapse
 
codeurabou profile image
Aboucodeur Author

Hello it just help you to understand basics of filter

Now it's your turn.

πŸ—’ Share a tutorial
πŸ€” Reflect on your coding journey
❓ Ask a question

Create an account to join hundreds of thousands of DEV members on their journey.