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 ]