0

So basically i have an array with an child array which looks like this

var test = [['code', 'name'], ['code', 'name'], ['code', 'name'], ['code', 'name'], ['code', 'name']];

and i have an input. After clicking submit, i need to check if the input value is already in the array and if so, display error message.

I already tried using this code, but it doesn't work. It doesn't show up the error

var value = labelInput.val();
if(jQuery.inArray(value, test) !== -1) {
    labelInput.addClass("sp-validation-error");
}
SpeekaDievs
  • 312
  • 2
  • 12
  • 1
    All child items in the test array is also an array. So you have to loop through each item in test array and then you can do `jQuery.inArray` to find if your input field value is exist in the test array or not. For ref [check this](https://api.jquery.com/jQuery.inArray/). – Deepak Biswal Jul 23 '15 at 10:36
  • possible duplicate of [array.contains(obj) in JavaScript](http://stackoverflow.com/questions/237104/array-containsobj-in-javascript) – Jason Cust Jul 23 '15 at 10:37

3 Answers3

3

Just loop through the child arrays and use the same method:

var value = labelInput.val(),
  found = false;

for (var i = 0; i < test.length; i++) {
  if (jQuery.inArray(value, test[i])) {
    alert('found!');
    found = true;
    break;
  }
}

if (found) {
  labelInput.addClass("sp-validation-error");
}

See also .indexOf

Alex
  • 9,911
  • 5
  • 33
  • 52
  • I don't quite understand the meaning of the index variable. Should i define it, or should this code work? – SpeekaDievs Jul 23 '15 at 10:42
  • This code should work. The index variable is a representative of each iteration within the test array. Have a look at javascript for loops. – Alex Jul 23 '15 at 13:58
  • Remember to use `(var index in test)` as opposed to `(index in test)` as you don't want to create a global variable! and it's also [a bad idea](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea) to use `for..in` on arrays rather than `for`. – lshettyl Jul 23 '15 at 14:16
2

Altough there is already an accepted answer, I feel there is an even more elegent way.

Instead of joining an array to a string and search that string, you can also use a one-liner using some:

if (test.some(function(element) { return element.indexOf(value) !== -1 })) {
  // do stuff
}
Alex
  • 9,911
  • 5
  • 33
  • 52
  • i know. you can use a polyfill or whatever. ms dropped support for ie8 so a reason for me not to bother anymore :) https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/some#Polyfill – Alex Jul 23 '15 at 14:41
1

You could also do the following way with no loops.

var value = labelInput.val();    
if( test.join(",").match(new RegExp(value, "gi")) !== null ) {
    labelInput.addClass("sp-validation-error");
}

Note: RegExp is a bit slower than indexOf. Hence, you could also use:

if( test.join(",").indexOf(value) !== -1 ) {
    labelInput.addClass("sp-validation-error");
}

A Demo

lshettyl
  • 8,166
  • 4
  • 25
  • 31
  • this is probably the most imperformant way of doing it. if you want to use string just use indexof rather than a regex, which is complete overkill – Alex Jul 23 '15 at 14:01
  • @Alex I have edited my answer to accommodate the _perfomant_ version as well. Thanks! – lshettyl Jul 23 '15 at 14:10