Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
search in array by the first two letters of any word within an array

using the following array, I want to search for the first letters assuming that there are several words in the same matrix, it must search in each word, if it finds it it should return in an array

example

const data= [ 
      "the lions of teranga",
      "tiger woods",
      "The Truman Show",
      "Shutter Island",
      "The Gold Rush",
  ]
]

if it matches "sh" it should search for each word and return

["Shutter Island", "The Truman Show"] but not The Gold Rush

if it matches "lions" it should search for each word and return

["the lions of teranga"]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Here is one of the solutions with combination of forEach and RegEx

const data= [ 
      "the lions of teranga",
      "tiger woods",
      "The Truman Show",
      "Shutter Island",
      "The Gold Rush",
  ]
  
const filteredData = []


data.forEach(sentence => {
    let words = sentence.split(" ")
    words.forEach((word,index) => {
      if(word.match(/^lions?\w/gi)) {
        filteredData.push(sentence)
      }
    })
    
})

console.log(filteredData)

about 4 years ago · Juan Pablo Isaza Report

0

const data= [ 
      "the lions of teranga",
      "tiger woods",
      "The Truman Show",
      "Shutter Island",
      "The Gold Rush",
  ]
inputData = "lions"
let a = data.filter(e => {
    str = e.toLowerCase()
    if (str.match(inputData.toLowerCase())){
        return e
    }
})
console.log(a)

Here filter returns the objects which are returned true. Here you can search by any word or character and return as an array.

about 4 years ago · Juan Pablo Isaza Report

0

//your data array
const data= [ 
      "the lions of teranga",
      "tiger woods",
      "The Truman Show",
      "Shutter Island",
      "The Gold Rush",
  ];
    //your search letters,if it will be the same use const instead of var
    var search="sh";
    //make your search case insensitive
    search=search.toLowerCase()
    //use for loop to go through every sentence
    for(var counter=0;counter<data.length;counter++)
    {
       //get the array of words
       var arrayOfWords=data[counter].match(/\b(\w+)\b/g);
       
       //use for loop to go through every word
       for(var counter2=0;counter2<arrayOfWords.length;counter2++)
       {
          //make word case insensitive
          var tempWord=arrayOfWords[counter2].toLowerCase();
          //check if the search length does not exid your actuall word
          if(search.length<=tempWord.length)
          {
             //check if first letters match
             if(tempWord.slice(0,search.length==search)
             {
                //first letters match, you can put your logic here
             }
             else
             {
                //no match
             }
          }
          else
          {
             //search exids word
          }
       }
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!