I know the title was a mouthful, but describes what I need to do here. I will admit I'm having a lot of trouble figuring this out!
I'm trying to create a discount calculator and this is my first time using arrays.
Here's the code I have so far...
dTrackerArray = [];
//Create Array
dTrackerArray.push( [var1, var2] );
//Add Variables to Array
var check = $.inArray(var1, dTrackerArray);
// Checking to see if variable exists, keeps returning -1!!!
alert(JSON.stringify(dTrackerArray));
// Even though I have this right under it which alerts the array clearly showing var 1 and var 2 exist!
Once I've figured out how to properly check the array, I'd like to retain the value of the pair (in a variable perhaps), and then remove the pair from the array.
I'm sorry If this was a little hard to read or understand!
You're creating a multidimensional array ( [var1, var2]
becomes an object within the dTrackerArray
array.)
Try pushing var1
and var2
separately.
dTrackerArray.push(var1);
dTrackerArray.push(var2);
You are adding an array that contains var1
and var2
to the dTrackerArray
array.
Use this syntax instead:
dTrackerArray.push(var1);
dTrackerArray.push(var2);
// Or this syntax
dTrackerArray.push(var1, var2);
// Or this syntax
dTrackerArray = [ var1, var2 ];
Here's a working fiddle to demonstrate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.