DEV Community

Aboucodeur
Aboucodeur

Posted on

1 1

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

Top comments (1)

Collapse
 
codeurabou profile image
Aboucodeur

Hello it just help you to understand basics of filter

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay