You could utilize a few builtin methods such as String#startsWith and Array#filter:
function search(arr, word) {
return arr.filter(element => element.toUpperCase().startsWith(word.toUpperCase()));
}
const myArr = ["england", "China", "France", "Eng", "Ch", "Australia"];
console.log(search(myArr, "e"));
The filter method only keeps elements in the array that meet the certain condition specified by the callback. Thus it checks if the element starts with the word regardless of case.
And if you need support for IE, try using String#indexOf:
element.toUpperCase().indexOf(word.toUpperCase()) == 0
This will check if the element starts with the specified string, regardless of case and is functionally equivalent.
Note that although it doesn't look pretty, the for loop is a better in terms of performance as estus noted. After testing it looks like it takes the for loop around 0.1 milliseconds to complete and my solution ranges from 0.2 to 0.7 milliseconds to complete. Though there's a difference, I wouldn't worry about performance until it really matters -- but the faster one here is your solution.
es6is a spec, not a function/method call. What do you mean by replace it withes6?Engnotenglandforis likely the most performant solution.