0

I have an sorted array e.g

var arr = [ "aasd","march","mazz" ,"xav" ];

And i want to find the first occurance of letter that starts with "m" here it would be 1 . Is thee any way how to do it without looping trought whole array?

5
  • Do you mean looping using for, while? What about map, forEach, filter? Commented May 17, 2016 at 14:10
  • Why don't you want to loop through the array? Any way you do this will have to loop through at least part of the array. Commented May 17, 2016 at 14:10
  • 1
    arr.find(s => s[0]==="m") Commented May 17, 2016 at 14:11
  • Or if you wanted the index: arr.findIndex(s => s[0]==="m") Commented May 17, 2016 at 14:17
  • Without looping, as of my knowledge, you cannot do it.. But with looping here is the solution Commented May 17, 2016 at 14:21

5 Answers 5

5

You could use a binary search to find any word starting with that letter, then loop backwards until you get the first one.

Sign up to request clarification or add additional context in comments.

Comments

5

Is there any way how to do it without looping trought whole array?

Yes, loop until you've found the match.

If you want to avoid a for or while construct, you can use Array's find() method.

For example, arr.find(word => word.startsWith("m")) should return the result you expect (or undefined if there's no such word).

Comments

3

You could use the find() function to search for the first match that meets your constraint.

The startsWith() function could easily handle this :

// Your array
var arr = [ "aasd","march","mazz" ,"xav" ];
// This will find the first match that starts with "m"
arr.find(function(word){ return word.startsWith('m');}); // yields "march"

Or if you needed a bit more extensive pattern matching, you could use a regular expression via the test() function, which can be seen in the following example and handles the same scenario (matching a string that begins with "m") :

// Your array
var arr = [ "aasd","march","mazz" ,"xav" ];
// First match that starts with "m" 
var match = arr.find(function(word){ return /^m/i.test(word);}); // yields "march"

Example

var arr = ["aasd", "march", "mazz", "xav"];
var match = arr.find(function(word) { return /^m/i.test(word); });
alert(match);

Comments

1

You dont need to loop through the whole array - only until such time as you find what you're interested in

function findFirstIndex(arr, char){
    for(var i=0;i<arr.length;i++){
       if(arr[i].substring(0,1) === char)
           return i;
    }
    return -1; // not found
}

Comments

0

You could use Array#some()

The some() method tests whether some element in the array passes the test implemented by the provided function.

function find(letter, array) {
    var index;
    array.some(function (a, i) {
        if (a[0] === letter) {
            index = i;
            return true;
        }
    });
    return index;
}

var arr = ["aasd", "march", "mazz", "xav"];

document.write(find('m', arr));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.