Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find and filter in javascript

const persons = [
  {name:"Shirshak",gender:"male"},
  {name:"Amelia",gender:"female"},
  {name:"Amand",gender:"male"}
]
//filter return all objects in array
let male = persons.filter(function(person){
return person.gender==='male'
})
console.log(male) //[{name:"Shirshak",gender:"male"},{name:"Amand",gender:"male"}]

//find return first object that match condition
let female = persons.find(function(person){
return person.gender==='female'
})
Comment

how to search and filter js

function myFunction() {
  // Declare variables
  var input, filter, ul, li, a, i, txtValue;
  input = document.getElementById('myInput');
  filter = input.value.toUpperCase();
  ul = document.getElementById("myUL");
  li = ul.getElementsByTagName('li');

  // Loop through all list items, and hide those who don't match the search query
  for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("a")[0];
    txtValue = a.textContent || a.innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript string length 
Javascript :: set cursor to end of input 
Javascript :: function create array javascript 
Javascript :: sum function in javascript 
Javascript :: javascript regex not in a set of characters 
Javascript :: response intersepters for axios create 
Javascript :: sequelize db:create test environment 
Javascript :: connect node with react 
Javascript :: javascript atan2 
Javascript :: multiple forms formData js 
Javascript :: Warning: Internal React error: Expected static flag was missing. Please notify the React team. 
Javascript :: server mail 
Javascript :: set number of reducers in mapreduce 
Javascript :: javascript get last 2 item in array 
Javascript :: how to add jquery to an html css and javascript project 
Javascript :: js ?. 
Javascript :: temporal dead zone in es6 
Javascript :: simple search filter for table html 
Javascript :: if statemnt shorthand js without else 
Javascript :: common javascript errors 
Javascript :: create variable javascript 
Javascript :: javascript number to string 
Javascript :: how to decode jwt token in react 
Javascript :: iterate table in jquery 
Javascript :: pwa clear cache 
Javascript :: js regexp match 
Javascript :: stripe delete product 
Javascript :: react native cli sdk.dir 
Javascript :: mongoose express js post 
Javascript :: javascript Recursionexample 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =