-4

I am struggling with how to check if an arry contains a value.

html:

    var newprodata = [];
    var alreadyUsedIds = [];
    angular.forEach(prodata, function(line) {       
        if(line.name === proName || line.brand === proBrand){
            //if (line.id === 183)  {
            var id = parseInt(line.id);
            console.log("line.id",id);
            console.log("alreadyUsedIds",alreadyUsedIds);
            console.log( "alreadyUsedIds.indexOf("+id+")", alreadyUsedIds.indexOf(id) );
            //}
            if (alreadyUsedIds.indexOf(id) > -1){
                console.log("on record pas");
            } else {
                //console.log("line.id",line.id);
                console.log("on record");
                alreadyUsedIds.push(parseInt(id));
                newprodata.push(line);
            }
        }
    });

here's my console output:

alreadyUsedIds [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
0: 24
1: 25
2: 26
3: 27
4: 28
5: 29
6: 30
7: 31
8: 32
9: 33
10: 34
11: 35
12: 36
13: 183
length: 14__proto__: Array[0]      filters.js:46 
alreadyUsedIds.indexOf(183) -1

, it should not be '-1' because '183' is in the array !!!

Moreover, if I set a breakpoint, and check manually I get: alreadyUsedIds.indexOf(183) = 13

Louis
  • 2,548
  • 10
  • 63
  • 120
  • It's not clear at all what that mix of code and output is trying to tell us. Please can you split it up into your code, expected output, actual output? – James M Nov 22 '15 at 13:12
  • there is an similar topic http://stackoverflow.com/questions/1181575/determine-whether-an-array-contains-a-value?answertab=active#tab-top and you could find answer there! – vlad sol Nov 22 '15 at 13:14
  • Are you looking at the value, before the array is updated or something like that ? – Sarath Chandra Nov 22 '15 at 13:15
  • I can't see `183` in `alreadyUsedIds`. I doubt you're retrieving the index in a loop, and `183` doesn't exist in the array at the time you're reading the index, but exists at the time console really logs the values. Notice, that console.log is asynchronous. Please show some real code you have. – Teemu Nov 22 '15 at 13:16
  • i think you should make a jsfiddle for better understanding – Kishore Sahasranaman Nov 22 '15 at 13:16
  • 1
    I don't see any actual code here, just what appears as some debug info. Can't really help without actual code. – Shadow The GPT Wizard Nov 22 '15 at 13:18
  • I added my code. There is a .push() on the array, into a angular.forEach...is there something asynchronous that I don't know about ? Because the issue is clearly that at the time of the indexOf(), 183 is not yet into the array...though it should be. – Louis Nov 25 '15 at 09:05

3 Answers3

1

You use alreadyUsedIds = [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36] However this array does not contain the element 183 so you wont find it there

Example on how it works:

var alreadyUsedIds = [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36];

// returns -1, because there is no element 183
console.log( alreadyUsedIds.indexOf(183) )

// add 183 to the end of the array
alreadyUsedIds.push(183) 

// returns 13, because the element 183 is at position 13
console.log( alreadyUsedIds.indexOf(183) )
CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • 1
    The `=` sign is not shown in the console for arrays. That code is the OP's console output, not actual code. – Jacques Marais Nov 22 '15 at 13:31
  • no because there's a weid behavior, at the time of the console output, 183 is not yet in the array – Louis Nov 25 '15 at 11:32
  • in my code? At the first output it is not in the array (as you can see the array does not contain 183 when its defined), but in the second it is (we added it to the array). – CoderPi Nov 25 '15 at 11:35
0

If alreadyUsedIds actually contains 183 it works fine, demonstration in jsfiddle:

https://jsfiddle.net/5qknqgex/

var alreadyUsedIds = [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 183];
console.log(alreadyUsedIds.indexOf(183));

The only explanation is that your array doesn't contain 183 at the time of console.log

Tim Sheehan
  • 3,994
  • 1
  • 15
  • 18
  • yes that's it, at the time of the console log, it's not yet in the array, but I don't really understand this. ... is .push() an async function ? – Louis Nov 25 '15 at 09:02
-1

it's working here

https://jsfiddle.net/jtc2wppu/2/

var alreadyUsedIds = [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 183];
console.log(alreadyUsedIds.indexOf(183));
stackoverfloweth
  • 6,669
  • 5
  • 38
  • 69