-2

I am using $.inArray to check if value is in array. if it is not I want to add it, else do nothing.

var arrayValues = [1];

for (var i = 1; i <= 3; i++) {

    if($.inArray('1', arrayValues) === -1) {

        arrayValues.push(0+i);  

    }

}

I am getting data on ajax page load as valuesArray = [value, value2]; on second load I get valuesArray valuesArray = [value, value2, value3, value4]; on third load I get value again and it still pushing it to valuesArray as valuesArray = [value, value2, value3, value4, value];.

Can anyone explain what I am doing wrong?

Thank you!

Please see live example at jsfiddle.

6
  • 1
    Is it 'value' or value ? Do you know that object1==object2 only if it's the same reference ? Please give us a working relevant code if you want an answer. Commented Jul 10, 2013 at 9:01
  • 1
    if('value', valuesArray)) === -1) What is supposed to do? Forgot to use $.inArray()?!
    – A. Wolff
    Commented Jul 10, 2013 at 9:02
  • Sorry, was writing quickly on a break. Fixed.
    – ignaty
    Commented Jul 10, 2013 at 9:08
  • There's still no visible relation between your code and the behavior you describe. Commented Jul 10, 2013 at 9:10
  • @ignaty could you answer first dystroy's question: it is value or 'value'?
    – A. Wolff
    Commented Jul 10, 2013 at 9:11

2 Answers 2

2

Your problem is that you add the number 1 in the array but check for the presence of the string "1". They're not equal for $.inArray.

You could do this :

if($.inArray(1, arrayValues) === -1) { // <- test with a number, not a string
    arrayValues.push(0+i); // note that the 0+ here is useless
}

While 1=='1', 1!=='1' and $.inArray tests using === as can be seen in the source code :

inArray: function( elem, arr, i ) {
    var len;

    if ( arr ) {
        if ( core_indexOf ) {
            return core_indexOf.call( arr, elem, i );
        }

        len = arr.length;
        i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;

        for ( ; i < len; i++ ) {
            // Skip accessing in sparse arrays
            if ( i in arr && arr[ i ] === elem ) {
                return i;
            }
        }
    }

    return -1;
},

More information on == vs === on the MDN.

0

Your code sample never calls $.inArray. It has syntax errors because there are multiple unclosed parens.

if($.inArray('value', valuesArray) === -1) {

    valuesArray.push(value);  

}
7
  • 1
    Shouldn't this be a comment ? Commented Jul 10, 2013 at 9:05
  • Sorry, was writing quickly on a break. Fixed.
    – ignaty
    Commented Jul 10, 2013 at 9:09
  • 2
    @dystroy It was an answer, but the question shifted on me. Commented Jul 10, 2013 at 9:13
  • 1
    You only noticed, like commentators, that there were obvious typos in the question. An answer is supposed to help solve the real problem, not point an error that isn't, obviously, in the real used code. Commented Jul 10, 2013 at 9:13
  • 3
    @KevinBowersox if you were a new user, this wouldn't be an issue. But you're a trusted user with over 20k rep. You're not supposed to do this kind of stupid stuff. Commented Jul 10, 2013 at 9:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.