0

I am trying to find an item in an array. I get only -1 for my variable a, so the item was not found in my array, but the item is definitely in array.

var sortiment = [];
var geschmack = [];
var kategorie = [];

function filterOptions(eigenschaft, filter){
    inhalt = filter + " = " + eigenschaft;
    console.log(inhalt);
    console.log(sortiment[0]);
    a = sortiment.indexOf(inhalt);
    console.log(a);

    switch(filter) {
        case "sortiment":
            sortiment.push([inhalt]);
            break;
        case "geschmack":
            geschmack.push([inhalt]);
            break;
        case "kategorie":
            kategorie.push([inhalt]);
            break;
        default:
            console.log("FAIL");
    }
}

In case the item is found, I want not to add it to the array.

dsh
  • 12,037
  • 3
  • 33
  • 51
Alex
  • 50
  • 1
  • 6
  • When `indexOf()` returns -1 then the value you searched for is not present. What is the output of `console.log(sortiment); console.log(inhalt);`? – dsh Aug 13 '15 at 18:57
  • 3
    You're pushing a (inner) array containing a single element (string) into the (outer) array, but then you're searching for the index of a string from the outer array. That's not going to work. In other words, the problem is most likely geschmack.push([inhalt]). Why are those square brackets there? – soapergem Aug 13 '15 at 18:59
  • 1
    @SoaperGEM Your comment should be an answer. – Siguza Aug 13 '15 at 19:00

2 Answers2

1

You're pushing an (inner) array containing a single element (string) into the (outer) array, but then you're searching for the index of a string from the outer array. That's not going to work. In other words, the problem is most likely this:

geschmack.push([inhalt]);

Why are those square brackets there? You probably want this:

geschmack.push(inhalt);

If you want to visualize this, your arrays will end up looking something like this:

[ ["filter1=eigenschaft1"], ["filter2=eigenschaft2"] ]

But you're not searching for ["filter1=eigenschaft1"]; you're searching for "filter1=eigenschaft1", so of course it won't find it. Alternatively you could change this line:

 a = sortiment.indexOf([inhalt]);

But this whole thing already seems a bit convoluted, to be honest.

soapergem
  • 9,263
  • 18
  • 96
  • 152
0

Your getting -1 because when you wrote var sortiment = []; that means it was not found in the array when you ran .IndexOf(something)

Here's a refrence : http://www.w3schools.com/jsref/jsref_indexof_array.asp

function filterOptions(eigenschaft, filter){
    inhalt = filter + " = " + eigenschaft;
    console.log(inhalt);
    console.log(sortiment[0]);
    switch(filter) {
        case "sortiment":
            sortiment.push(inhalt);//remove [ ]
            break;
        case "geschmack":
            geschmack.push(inhalt);
            break;
        case "kategorie":
            kategorie.push(inhalt);
            break;
        default:
            console.log("FAIL");
    }
    a = sortiment.indexOf(inhalt); //look for index after .push
    console.log(a);
}
Page
  • 63
  • 1
  • 6
  • Looks like you missed the `sortiment.push` later on. – Siguza Aug 13 '15 at 19:00
  • But you still get -1 from a because you looked for the index first if you were to do `.push` first then do `.IndexOf()` then a would be 0 because it is the first element in the array. – Page Aug 13 '15 at 19:11
  • I was working on the assumption that `filterOptions` gets called multiple times, but you're right, we have no evidence that this is actually the case... – Siguza Aug 14 '15 at 06:43