-1

I have this array:

var str = "rrr";
var arr = ["ddd","rrr","ttt"];

I try to check If arr contains str. I try this:

  var res = arr .find(str);

But on row above is not works any idea what I do wrong?

Michael
  • 13,950
  • 57
  • 145
  • 288
  • What are you expecting in response ? – Rayon Sep 13 '16 at 06:53
  • @Rayon no string exists I expect null if exists I expect true – Michael Sep 13 '16 at 06:54
  • 2
    Check `console` for errors... `Array#find` expects argument as `function`, not as `value`.. – Rayon Sep 13 '16 at 06:54
  • `arr.find( x => x === str );` – Wiktor Zychla Sep 13 '16 at 06:55
  • As per @Rayon: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find – jedifans Sep 13 '16 at 06:55
  • If, as per your comment, you want a `true` or `null` result depending on whether the item exists (as compared to getting back the index), maybe you should [edit] your question to mention that, because it opens up options like `.includes()`. – nnnnnn Sep 13 '16 at 07:02
  • Possible duplicate of [How to find if an array contains a specific string in JavaScript/jQuery?](http://stackoverflow.com/questions/6116474/how-to-find-if-an-array-contains-a-specific-string-in-javascript-jquery) – Makyen Sep 13 '16 at 07:02
  • Another time, please use a search engine to at least make an attempt at searching for an answer prior to asking a question. The first result for a Google search for [Find string in array](https://www.google.com/search?q=Find+string+in+array) is a duplicate of your question. – Makyen Sep 13 '16 at 07:05
  • If your question is really how to use [Array.prototype.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) then please [edit] your question to reflect that. If that is your question, please explain what it is about the documentation for that method which you don't understand. – Makyen Sep 13 '16 at 07:08
  • @Michael: *"no string exists I expect null if exists I expect true"* Why would you want `null` rather than `false`? – T.J. Crowder Sep 13 '16 at 07:15

5 Answers5

5

Try using indexOf: it's more widely supported and you can get the same results.

var index = arr.indexOf(str);
if(index != -1) {
  console.log("found");
  var element = arr[index]; //element found of your array
}

Your problem with find function it's probably due to it's compatibility:

enter image description here

As you can see, the chance you are using an incompatible browser it's not so far.

Community
  • 1
  • 1
illeb
  • 2,942
  • 1
  • 21
  • 35
3

find (added in ES2015, aka "ES6") expects a function predicate. You're looking for indexOf:

var res = arr.indexOf(str);

...which finds things by comparing them with ===. You'll get -1 if it's not found, or its index if it is.

In a comment you've said:

no string exists I expect null if exists I expect true

...which seems a bit odd (I'd think you'd want true or false), but this will give you that:

var res = arr.indexOf(str) != -1 || null;

...because of JavaScript's curiously-powerful || operator (that's a post on my blog).


Just for completeness about find:

But you can use find; ES5 version (in case you're polyfilling but not transpiling):

var res = arr.find(function(entry) { return entry === str; });

or in ES2015:

let res = arr.find(entry => entry === str);

res will be null if it was not found, str if it was. But then, you already have str, so... :-) find is more useful when you're searching for, say, an object by a property value.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • *"**or** in ES2015"* - But you already said that `.find()` is an ES2015 method... – nnnnnn Sep 13 '16 at 06:59
  • @nnnnnn: Yes, but it can trivially be polyfilled. I was polyfilling it for fully a year before I started transpiling. – T.J. Crowder Sep 13 '16 at 07:00
  • 1
    Fair enough. As always you've covered everything well (though I also upvoted the answer that suggested `.includes()`, because the OP commented above that their desired result is `true` if the item exists and `null` if it doesn't). – nnnnnn Sep 13 '16 at 07:09
  • @nnnnnn: I did too. I don't see that in the question (and `includes` doesn't return `null` if the item isn't in the array). – T.J. Crowder Sep 13 '16 at 07:11
2

It's easy to use simple indexOf method. Just check the documentation.

var array = [2, 9, 9];
array.indexOf(2);     // 0
array.indexOf(7);     // -1
array.indexOf(9, 2);  // 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0
Rashad Ibrahimov
  • 3,279
  • 2
  • 18
  • 39
2

.find method of array prototype expects on input function callback, not string you are looking for.

var res = arr.find(function (element) {
  return element === str;
};

Read more about it here.

Paweł Mikołajczyk
  • 371
  • 1
  • 2
  • 14
2

Try using includes

var foo = ['aaa', 'bbb', 'ccc'];
console.log(foo.includes('aaa'));
console.log(foo.includes('ddd'));

output

true
false

Note: Array#includes wasn't added until ES2016 (in June 2016), so you need a polyfill/shim for even slightly out of date browsers (which is simple, there's one on MDN).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
pius lee
  • 1,164
  • 1
  • 12
  • 28
  • 1
    Good point, `includes` is a good fit for the "does it exist" use case. But it's important to note that `includes` wasn't added until June 2016 (e.g., just over two months ago), so you need a polyfill/shim (which is trivial) for even *slightly* out of date browsers. – T.J. Crowder Sep 13 '16 at 07:10
  • 1
    Or `foo.includes('aaa') || null` to get the `true` or `null` result that the OP mentioned in a comment. – nnnnnn Sep 13 '16 at 07:13