I just found out that using the function array.push(value) using the same value name, will give the same index of the first item with the same value name.
Look at the example code following:
<p>Click the button to add a new element to the array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.push("Orange");
var orange_index = fruits.indexOf("Orange");
document.getElementById("demo").innerHTML = orange_index;
}
</script>
If you run it, you will notice that the answer is "1" , that corrisponds to the first "Orange" item inserted into the array.
How can I avoid this behaviour ? I need to insert the same values into an array with different indexes.
Do I need to use multidimensional array ? I have a deck of cards, and I need to store the ranks and suits in array. So far I created two different arrays for ranks and suits, but I'm having this problem. I retrieve the ranks and suits separately, should I push them together into a new multidimensional array? how ?
Thanks