I have tried this code to find if some object already exists in Array, but, it is not working:
function lote(pnIdLote,psNmLote, pnIdQuadro){
this.idlote = pnIdLote;
this.nmlote = psNmLote;
this.idquadro = pnIdQuadro;
}
Array.prototype.contains = function(obj){
var i = this.length;
while(i--){
console.log(i);
if (this[i] == obj) return true;
}
return false;
};
My code:
$(data).each(function(index){
if(this.idlote!=""){
loLote = new lote(this.idlote, this.nmlote, this.quadro);
if(!laLote.contains(loLote)){
laLote.push(loLote);
}
}
});
console.log(laLote);
The array appears this way:
[
lote
idlote: "2"
idquadro: "1"
nmlote: "Lote 1"
__proto__: lote
,
lote
idlote: "2"
idquadro: "1"
nmlote: "Lote 1"
__proto__: lote
,
lote
idlote: "2"
idquadro: "1"
nmlote: "Lote 1"
__proto__: lote
]
And I'd like an array with only one element, like this:
[
lote
idlote: "2"
idquadro: "1"
nmlote: "Lote 1"
__proto__: lote
]
Where do I fix my code?
Thanks, Luciano