0
I need to know if an array in Javascript has duplicate elements.
Do you have a function in the jQuery API that does this? If not, how do I proceed?
0
I need to know if an array in Javascript has duplicate elements.
Do you have a function in the jQuery API that does this? If not, how do I proceed?
4
With ES6 magic you can solve your problem with just one line of code. Ex:
var a = ["Ceará","Eusébio","Ceará"];
var b = ["JavaScript",">","All"];
function hasDuplicates(array) {
return (new Set(array)).size !== array.length;
}
hasDuplicates(a);//true
hasDuplicates(b);//false
3
No, there is no function via jQuery that does this.. the closest to that reality would be the .Unic(), but only works with elements DOM
.
But you can develop your own method using .each
and .inArray
.
Take a look:
var times = ["Flamengo","Vasco","Corinthians","Fluminense","Corinthians","Fluminense","Palmeiras","Vasco"];
var timesNaoDuplicados = [];
$.each(times, function(i, elemento){
if($.inArray(elemento, timesNaoDuplicados) === -1) timesNaoDuplicados.push(elemento);
});
//Se "printar" o timesNaoDuplicados: ["Flamengo", "Vasco", "Corinthians", "Fluminense", "Palmeiras"]
If you want to know only if a element is inside an array, use the .inArray(elemento, Array)
.
More about the .inArray
here.
In summary, it returns you the position (index) where the element is inside the array. If you find nothing, returns -1.
[SOLVED] Your code does not answer me, because I need to know which elements are duplicated. So I already solved. I found the answer on the site :http://www.lucaspeperaio.com.br/blog/funcao-javascript-para-eliminar-duplicidade-em-array . This returns the duplicate elements in the array But thanks for your attention. The following is the code below: <code> Array.prototype.Duplicates = Function (){ Return this.filter(Function(x,y,k){ Return y !== k.lastIndexOf(x) ; }) ; } var duplicates = ['a','b','c',’d','a',','b']. Duplicates();
[SOLVED] Your code does not answer me, because I need to know which elements are duplicated. So I already solved. I found the answer on the website [link] (http://www.lucaspeperaio.com.br/blog/funcao-javascript-para-eliminar-duplicidade-em-array)[/link] . This returns the duplicate elements in the array But thanks for your attention. The following code is: 'codigo Array.prototype.Duplicates = Function(){ Return this.filter(Function(x,y,k){ Return y !== k.lastIndexOf(x) ; }) ; } var duplicates = ['a','b','c',’d',’d'a','b']. Duplicates(); '/code'
code on ideone: return only the duplicate values of an array
Browser other questions tagged javascript jquery array
You are not signed in. Login or sign up in order to post.
What have you tried ? has some example ?
– Gabriel Rodrigues
What do you want to 'do' with duplicates? Remove from array? Save to another array? Just print to console?
– Jéf Bueno
Maybe this question will help you: http://answall.com/questions/99193/valor-em-array-com-a-maior-ocorr%C3%Aancia
– Lucas Fontes Gaspareto
To Answer is here, the variable vector, is where would have the elements.
– Ivan Ferrer