JavaScript

June 3, 2023

How to use JavaScript Array filter()

Program to use JavaScript Array filter() method


let a = ['Apple', 'Mango', 'Banana', 'Orange', 'Pomegranate'];

let b = a.filter(a => a.length > 5);

console.log(b);

Output


[ 'Banana', 'Orange', 'Pomegranate' ]

Program to use JavaScript Array filter() method


let c = ['Apple', 'Mango', 'Banana', 'Orange', 'Pomegranate'];

let d = ['Orange', 'Apple', 'Banana']

let e = (c, d) => {
    let value = [...c, ...d];
    return value.filter((el) => {
        return !(c.includes(el) && d.includes(el));
    });
};

console.log(e(c, d));

Output


[ 'Mango', 'Pomegranate' ]

Program to use JavaScript Array filter() method


let f = [1, 2, 3, 4];
let g = [2, 1, 8, 3];
let h = (f, g) => {
  let value = [...f, ...g];
  return value.filter((el) => {
    return !(f.includes(el) && g.includes(el));
  });
};
console.log(h(f, g));

Output


[ 4, 8 ]

by : Suhel Akhtar

Quick Summary:

In first example javascript Array filter() method if array length is more than 5 characters then print
In the second example, when comparing two arrays, the value that does not match is printed