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'
})
// 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"]
const persons = [
{name:"Shirshak",gender:"male"},
{name:"Amelia",gender:"female"},
{name:"Amand",gender:"male"}
]
//filter return all objects in array
let male = persons.filter(person=>person.gender==='male')
console.log(male)
// 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]