searching a string in array by partial of it

87 Views Asked by At

I wanna find a string inside an array by some part of its name(at least more than 3characters), Like: Buffalo is an index of array and I want to search buf or buff, buffa, buffal or even the hole word(buffalo) and it return Buffalo. No matter if its case-sensitive or NOT

here is my array:

const carsName =
      [
          "Landstalker",
          "Bravura",
          "Buffalo",
          "Linerunner",
          "Perrenial",
          "Sentinel",
          "Dumper",
          "Firetruck",
          "Trashmaster",
          "Stretch",
          "Manana",
          "Infernus"
      ]

I wrote it in PAWN once and I'm gonna write it in JS (for my Alt V server) but I'm stuck at this point, is it possible to do?

and at the end, sorry for my English

1

There are 1 best solutions below

3
On

Case insensitive partial match:

const carsName = [
  "Landstalker",
  "Bravura",
  "Buffalo",
  "Linerunner",
  "Perrenial",
  "Sentinel",
  "Dumper",
  "Firetruck",
  "Trashmaster",
  "Stretch",
  "Manana",
  "Infernus",
];

const partial = 'buf';

const foundName = carsName.find(
  name => name.toLowerCase().includes(partial.toLowerCase())
);

console.log(foundName);