Overwrite Duplicate Value In An Array - Javascript
I have read some partially solutions on my problem but unfortunately I've come up for questioning. So here's my question. I have an array var = results[1,2,2,3,1,3]. I have to prin
Solution 1:
Here is the very simple implementation using js
var getfruits = [1, 2, 2, 3, 1, 3];
var uniq = [];
for (var i = 0; i < getfruits.length; i++) {
if(uniq.indexOf(getfruits[i])==-1)
uniq.push(getfruits[i]);
};
console.log(uniq);
And you have to just add lo-dash
in your fiddle.
Solution 2:
Try this
var getfruits = [1, 2, 2, 3, 1, 3];
var newfruits = [];
$.each(getfruits, function (i, el) {
if ($.inArray(el, newfruits) === -1) newfruits.push(el);
});
console.log(newfruits)
alert(newfruits[0]);
alert(newfruits[1]);
alert(newfruits[2]);
Solution 3:
Try this
var arr = [1, 2, 2, 3, 1, 3];
var newarr = [];
$.each(arr, function (index, element) {
if ($.inArray(element, newarr) < 0) newarr.push(element);
});
alert(newarr[0]);
alert(newarr[1]);
alert(newarr[2]);
Post a Comment for "Overwrite Duplicate Value In An Array - Javascript"