FIlter array or return full array

PHOTO EMBED

Mon Sep 04 2023 05:13:11 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #javascript #array #filter

function filterAndCheckValue(array, valueToCheck) {
  const filteredArray = array.filter(item => item === valueToCheck);

  if (filteredArray.length === 0) {
    return array;
  }

  return filteredArray;
}

const myArray = [1, 2, 3, 4, 5];
const valueToFind = 6;

const result = filterAndCheckValue(myArray, valueToFind);
console.log(result); // Output: [1, 2, 3, 4, 5]
content_copyCOPY

filter through an array and check value exists, if not, give me the entire array