'Array as argument and using the array in the new function in javascript

var filterKeys = ["hello this is javascript", "Movies"];
var title= "Hello"
searchKeyInNo = keySearch(filterKeys, title)

function KeySearch(filteredArray, searchKey) {
    var flag=0
    for (var i = 0; i<filteredArray.count; i++) {
        //array operations
        flag=1
    }
    return flag
}
var filterKeys = ["hello this is javascript", "Movies"];
var title= "Hello"
searchKeyInNo = keySearch(filterKeys, title)

function KeySearch(filteredArray, searchKey) {
    var tempArray = filteredArray
    var flag=0
    for (var i = 0; i<tempArray.count; i++) {
        //array operations
        flag=1
    }
    return flag
}

Can we use passed array as paramneter as a function array?
Can we pass and use the entire array as argument?
I even tried the second piece of code that I added a new array to copy the argument array to process the function scoped array.

UPDATE: VS Code never showed up suggestion for argumentarray.length. Thanks. WORKS NOW!



Solution 1:[1]

Try this. You were using .count instead of .length on the filtered array to get the count of elements.

function KeySearch(filteredArray, searchKey) 
{

    var flag=0
    for (var i = 0; i<filteredArray.length; i++)
    {
       if(filteredArray[i].toLowerCase()
           .includes(searchKey.toLowerCase()))
       {
          flag++
       }
    }
   return flag
};

console.log(KeySearch(["hello this is javascript", "Movies"], "Hello"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1