-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?

10
  • What are you expecting in response ? Commented Sep 13, 2016 at 6:53
  • @Rayon no string exists I expect null if exists I expect true Commented Sep 13, 2016 at 6:54
  • 2
    Check console for errors... Array#find expects argument as function, not as value.. Commented Sep 13, 2016 at 6:54
  • arr.find( x => x === str ); Commented Sep 13, 2016 at 6:55
  • As per @Rayon: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 13, 2016 at 6:55

5 Answers 5

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.

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

Comments

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.

4 Comments

"or in ES2015" - But you already said that .find() is an ES2015 method...
@nnnnnn: Yes, but it can trivially be polyfilled. I was polyfilling it for fully a year before I started transpiling.
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: 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).
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

Comments

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.

Comments

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).

2 Comments

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.
Or foo.includes('aaa') || null to get the true or null result that the OP mentioned in a comment.

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.