Example: I have an array with repeated values 1 and 2
[1,1,2,2,3,4,5]
I want the result of that array to be an array of the values that dont repeat.
[3,4,5]
Example: I have an array with repeated values 1 and 2
[1,1,2,2,3,4,5]
I want the result of that array to be an array of the values that dont repeat.
[3,4,5]
var arr = [1,1,2,2,3,4,5]
arr = arr.filter (function (value, index, array) {
return array.indexOf (value) == array.lastIndexOf(value);
});
console.log(arr);
Without JQuery, you can use the filter method:
var nums = [1,1,2,2,3,4,5]
nums = nums.filter(function(val){
return nums.indexOf(val)===nums.lastIndexOf(val);
});
// [3,4,5]
Otherwise, if in future you want to preserve repeated numbers, you can use:
for(var i=0; i<nums.length; i++) if(i!==nums.lastIndexOf(nums[i])) nums.splice(i, 1);
// [1,2,3,4,5]