JavaScript - Find a word string out of strings in an array

I am trying to find a word from consecutive strings inside an array and I am stuck at the moment.

For example: array = ['w', 'r', 'a', 'p', 'p', 'l', 'e', 'f', 'k', 'l']; I want to make a function that will return true if the word 'apple' is inside this array. Strings need to be consecutive.

array1 = ['w', 'r', 'a', 'p', 'l', 'p', 'e', 'f', 'k', 'l']; function for this kind of strings with no consecutive 'apple' strings should return false.

Can you help please?

7 answers

  • answered 2018-03-11 12:41 Danyal Imran

    One approach would be traverse your array, and get substrings of length that equals the length of the matching substring, and compare the substring with the matching substring

    for (int i=0; i<array.length-matching.length; i++) {
        if (array.substr(i, i+matching.length) == matching) return true;
    } return false;

  • answered 2018-03-11 12:41 Dan Kreiger

    Try this:

    var array = ['w', 'r', 'a', 'p', 'p', 'l', 'e', 'f', 'k', 'l'];
    function findWord(arr, word) {
      return arr.join('').indexOf(word) !== -1;
    // test
    console.log('apple: ', findWord(array, 'apple'))
    console.log('dog: ', findWord(array, 'dog'))

  • answered 2018-03-11 12:41 scraaappy

    array = ["w", "r", "a", "p", "p", "l", "e", "f", "k", "l"];

  • answered 2018-03-11 12:41 Piotr P.

    check this function:

    function hasWord(inputSourceList, inputWord) {
        return inputSourceList.join('').indexOf(inputWord) > -1;

  • answered 2018-03-11 12:41 RomanPerekhrest

    With Array.join(), String.replace() and String.indexOf() functions:

    var contains_word = function(arr, w) {
        return arr.join('').replace(new RegExp('[^' + w + ']', 'g'), '')
               .indexOf(w) !== -1;	
    console.log(contains_word(['w', 'r', 'a', 'p', 'l', 'p', 'e', 'f', 'k', 'l'], 'apple'));
    console.log(contains_word(['w', 'r', 'a', 'p', 'p', 'l', 'e', 'f', 'k', 'l'], 'apple'));

  • answered 2018-03-11 12:41 rainmak3r

    Another approach can be using includes.

    var arr = ['a','b','c','a','P','p','l','E'];
    function includesWord(sourceArr, word){
      return sourceArr.join('').toLowerCase().includes(word.toLowerCase());
    console.log(includesWord(arr, 'APPLE'));

  • answered 2018-03-11 12:41 Ori Drori

    You can use a combination of Array.every(), and Array.indexOf() to check if all letters are found in the correct order:

    const findWordInArray = (word, array) => {
      let last = -1; // stores the last index found
      // iterate the letters of the word
      return [...word].every((c) => {
        // search for the letter from the last known position + 1 onwards
        const index = array.indexOf(c, last + 1);
        last = index; // change last to the new found position
        return index !== -1;
    const array1 = ['w', 'r', 'a', 'p', 'p', 'l', 'e', 'f', 'k', 'l'];
    const array2 = ['w', 'r', 'a', 'p', 'l', 'p', 'e', 'f', 'k', 'l'];
    const word = 'apple';
    console.log(findWordInArray(word, array1));
    console.log(findWordInArray(word, array2));