0

I have an array. And i'd like to check if that index or that element of array contains the value of a string or a number:

var arrayArray = ['cool bmw', 'wew toyota', 'try honda'];

I'd like to check if some value inside of the element is existing.

Example on index 2 i'd like to check whether it contains the word toyota.

  • Hi Anon, which programming language are you using? What have you tried so far? – hakamairi Jun 28 '18 at 10:52
  • 1
    Possible duplicate of [How do I check if an array includes an object in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – Søren D. Ptæus Jun 28 '18 at 11:08
  • @Anon Is your question about JavaScript? Your comments on Arvind's answer seem to imply this. Can you please confirm? – Søren D. Ptæus Jun 28 '18 at 11:13

1 Answers1

0

Example on index 2 i'd like to check whether it contains the word toyota.

You need String.indexOf():

var arrayArray = ['cool bmw', 'wew toyota', 'try honda'];
var checkWord = 'toyota';
var position = arrayArray[1].indexOf(checkWord);
var found = (-1 !== position);
console.log('found = ',found);

Here, indexOf() will return -1 if no match found else will return index position of first occurrence starting from 0.

What if i didn't indicate the [1] i'd like to check it in a for loop how do i check on that?

var arrayArray = ['cool bmw', 'wew toyota', 'try honda'];
var checkWord = 'toyota';
var found = false;
for (var i = 0; arrayArray.length > i; i++) {
  if (-1 !== arrayArray[i].indexOf(checkWord)) {
    found = true;
    break;
  }
}
console.log('found =', found);