Example 1: filtering json array in javascript
const data = [
{
name: 'Bob',
gender: 'male',
age: 34,
},
{
name: 'Carol',
gender: 'female',
age: 36,
},
{
name: 'Ted',
gender: 'male',
age: 38,
},
{
name: 'Alice',
gender: 'female',
age: 40,
},
];
const arr1 = data.filter(d => d.age > 37);
console.log('arr1', arr1);
const arr2 = data.filter(d => d.gender === 'female');
console.log('arr2', arr2);
const ageAndGender = d => d.age > 37 && d.gender === 'female';
const arr3 = data.filter(ageAndGender);
console.log('arr3', arr3);
Example 2: who to accses to an object vallue inside an array with .filter method js
const array = [
{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
const filterArray=array.filter(word=>word.team==="red")
Example 3: filter javascript array
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
Example 4: how the filter() function works javascript
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const filter = arr.filter((number) => number > 5);
console.log(filter);
Example 5: .filter js
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
Example 6: javascript filter
const filtered = array.filter(item => {
return item < 20;
});