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'
})
const filtered = array.filter(item => {
return item < 20;
});
// An example that will loop through an array
// and create a new array containing only items that
// are less than 20. If array is [13, 65, 101, 19],
// the returned array in filtered will be [13, 19]
// The filter() method creates a new array with all elements
// that pass the test implemented
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
// filter(): returns a new array with all elements that pass the test
// If no elements pass the test, an empty array will be returned.
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
const oddFiltration = (arr) => {
const result = arr.filter(n => n%2);
return (result);
}
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
/* Here's an example that uses (some) ES6 Javascript semantics to filter an object array by another object array. */
// x = full dataset
// y = filter dataset
let x = [
{"val": 1, "text": "a"},
{"val": 2, "text": "b"},
{"val": 3, "text": "c"},
{"val": 4, "text": "d"},
{"val": 5, "text": "e"}
],
y = [
{"val": 1, "text": "a"},
{"val": 4, "text": "d"}
];
// Use map to get a simple array of "val" values. Ex: [1,4]
let yFilter = y.map(itemY => { return itemY.val; });
// Use filter and "not" includes to filter the full dataset by the filter dataset's val.
let filteredX = x.filter(itemX => !yFilter.includes(itemX.val));
// Print the result.
console.log(filteredX);
Run code snippet
// let arr = [1,2,3]
/*
filter accepts a callback function, and each value of arr is passed to the
callback function. You define the callback function as you would a regular
function, you're just doing it inside the filter function. filter applies the code
in the callback function to each value of arr, and creates a new array based on your
callback functions return values. The return value must be a boolean, which denotes whether an element
should be keep or not
*/
let filteredArr = arr.filter(function(value){
return value >= 2
})
// filteredArr is:
// [2,3]
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// function to check even numbers
function checkEven(number) {
if (number % 2 == 0)
return true;
else
return false;
}
// create a new array by filter even numbers from the numbers array
let evenNumbers = numbers.filter(checkEven);
console.log(evenNumbers);
// Output: [ 2, 4, 6, 8, 10 ]
# filter function
l1=[10,20,30,40,50,60,70,80]
l2= [i for i in filter(lambda x:x>30,l1)]
print(l2)
# [40, 50, 60, 70, 80]
# filter takes a function and a collection. It returns a collection of every item for which the function returned True.
// filter takes an array and function as argumentfunction
filter(arr, filterFunc) {
const filterArr = []; // empty array
// loop though array
for(let i=0;i<arr.length;i++) {
const result = filterFunc(arr[i], i, arr);
// push the current element if result is true
if(result)
filterArr.push(arr[i]);
}
return filterArr;
}
/* What is the filter() method?
This method returns all the elements of the array that satisfy the condition
specified in the callback function.
*/
const x = [1, 2, 3, 4, 5];
const y = x.filter(el => el*2 === 2);
console.log("y is: ", y); // y is: [1]
/* If you check out the output of the above example,
the value of y is an array of 1 element that satisfies the condition.
This is because the filter() method iterates over all elements of the array and
then returns a filtered array which satisfy the condition specified.
*/