array filter how it works code example

Example 1: the filter array

var id = 2;
var list = [{
  Id: 1,
  Name: 'a'
}, {
  Id: 2,
  Name: 'b'
}, {
  Id: 3,
  Name: 'c'
}];
var lists = list.filter(x => {
  return x.Id != id;
})
console.log(lists);

Example 2: filter array by keyword

var data = [
  {email: "[email protected]",nama:"User A", Level:"Super Admin"},
  {email: "[email protected]",nama:"User B", Level:"Super Admin"},
  {email: "[email protected]",nama:"User C", Level:"Standart"},
  {email: "[email protected]",nama:"User D", Level:"Standart"},
  {email: "[email protected]",nama:"User E", Level:"Admin"},
  {email: "[email protected]",nama:"User F", Level:"Standart"}
];
var filter = "Level";
var keyword = "Standart";

var filteredData = data.filter(function(obj) {
	return obj[filter] === keyword;
});

console.log(filteredData);

Example 3: filter an array in Javascript

1let greaterTen = [];2
3for (let i = 0; i<numbers.length; i++) {4  var currentNumber = numbers[i];5  if (currentNumber > 10) {6    greaterTen.push(currentNumber)7  }8}9
10console.log(greaterTen); // [23, 12, 45, 78, 11, 10.1, 84]

Tags:

Css Example