Search in JS

Andrijan Portrait

Andrijan Tasevski ยท 12 Oct, 2022

const arr = [
  { name: "John Mulaney" },
  { name: "Bill Burr" },
  { name: "Tom Segura" },
];

const query = "John Mulaney";

const filteredArr = arr.filter((item) =>
  item.name.toLowerCase().includes(query.toLowerCase())
); // [{name: "John Mulaney"}]
const arr = [
  { name: "John Mulaney" },
  { name: "Bill Burr" },
  { name: "Tom Segura" },
];

const query = "John Mulaney";

const filteredArr = arr.filter(
  (item) =>
    query.toLowerCase() === item.name.toLowerCase().substring(0, query.length)
); // [{name: "John Mulaney"}]