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

array.filter in javascript

//How To Use Array.filter() in JavaScript
//example 1. 
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length < 6);

console.log(result);

//OUTPUT: ['spray', 'limit', 'elite'

//example 2
const numbers = [45, 4, 9, 16, 25];
const over18 = numbers.filter(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: material ui outlined input with icon 
Javascript :: reverse every word in a string javascript 
Javascript :: how to get thumbnail image from video file in javascript 
Javascript :: keydown events 
Javascript :: export all javascript 
Javascript :: redondear decimales javascript 
Javascript :: document.append 
Javascript :: execute a function at a certain time of day js 
Javascript :: js how to round up 2 decimal places 
Javascript :: js platformer 
Javascript :: javascript merge array 
Javascript :: The jQuery noConflict() Method 
Javascript :: input event on value changed 
Javascript :: json query online 
Javascript :: react router dom private route 
Javascript :: useNavigate history back 
Javascript :: change navigation animation react native 
Javascript :: js get class property 
Javascript :: tab adds tab textarea javascript 
Javascript :: remove comma from string jquery 
Javascript :: node.js util module 
Javascript :: vuejs cant add script in template 
Javascript :: express router file 
Javascript :: Find the maximum number in a jagged array of numbers 
Javascript :: javascript onclick button 
Javascript :: run onclick function once 
Javascript :: mongodb push to index 
Javascript :: moment get day 
Javascript :: html2pdf example angular 
Javascript :: angular capitalize first letter 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =