0

I need your help,

I am try to match the keyword "BN to" but it appears that this is not working out for me, the result is always "-1". What am I doing wrong?

var yourArray = ["BNI to ALPHA", "BNI to BRAVO", "BNI to CHARLIE"]

var found = $.inArray('BNI to', yourArray)

alert(found)

}
1
  • You are searching for a member inside the array with a string like this "Bni to" and theres no member with that specific string. Commented Jan 18, 2016 at 20:58

1 Answer 1

1

The $.inArray() function tells you where a given value can be found the array. Your string is not equal to any member of the array, so the return value is -1.

I think what you're looking for could be Array.prototype.findIndex:

var found = yourArray.findIndex(function(s) {
  return s.startsWith("BNI to");
});

The .findIndex() function is an ES2015 thing, and not supported by Internet Explorer yet (according to MDN). There's a Polyfill to use however.

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

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.