UPDATE
Here is a fiddle of the problem: https://jsfiddle.net/q9c5fku3/ when I run that code and look at the console I see it's console.logging a different number in the array.
Thanks for your replies, sorry I'm getting downvotes but this is really confusing me.
I've tried it again using different numbers, I'm wondering can you guys test these numbers on your end and see if you get a different result?
var myArray = [621608617992776, 621608617992776, 10156938936550295, 621608617992776, 10156938936550295, 10156938936550295, 621608617992776, 10156938936550295];
console.log(myArray);
var myArrayTrimmed = [];
for(var i in myArray){
if(myArrayTrimmed.indexOf(myArray[i]) === -1){
myArrayTrimmed.push(myArray[i]);
}
}
console.log(myArrayTrimmed);
This is giving me the following array in the console:
[621608617992776, 10156938936550296]
For some reason the second number has increased by 1.
====================
Original Question:
I have this array:
var myArray = [100, 200, 100, 200, 100, 100, 200, 200, 200, 200];
I'm trying to create a new array named myArrayTrimmed
that will be the same as the above array, except it will have duplicates removed. This should result in:
var myArrayTrimmed = [100, 200];
This is the code I'm using to try to achieve this:
var myArray = [100, 200, 100, 200, 100, 100, 200, 200, 200, 200];
var myArrayTrimmed = [];
for(var i in myArray){
if(myArrayTrimmed.indexOf(myArray[i]) === -1){
myArrayTrimmed.push(myArray[i]);
}
}
console.log(myArrayTrimmed);
This isn't working correctly, while it is removing duplicates, for some reason it's subtracting the number 1
from 200, so the output in the console is:
[100, 199]
I think this must be due to the -1
in the code, but I don't know how else to remove the duplicates.