Struggling to find equal values in my array, I've attempted several methods without success.
One approach I tried involved sorting the array:
var sorted_arr = this.variacaoForm.value.variacoes.sort(); // the comparing function here
for (var i = 0; i < this.variacaoForm.value.variacoes.length - 1; i++) {
if (sorted_arr[i + 1].sku == sorted_arr[i].sku) {
console.log('Found equal elements in the array');
}
}
However, this method didn't catch the equality between the first and third elements.
I also attempted another method:
for(let i=0;i<this.variacaoForm.value.variacoes.length;i++){
if(this.produto.sku_prin == this.variacaoForm.value.variacoes[i].sku){
console.log('Equal values found in the array');
}
}
But, even with this approach, the console would log equal values even when none were present.
Lastly, I explored a different strategy:
for (let i = 0; i < this.variacaoForm.value.variacoes.length-1; i++) {
for (let j = i+1; j < this.variacaoForm.value.variacoes.length; j++) {
if (this.variacaoForm.value.variacoes[i].sku === this.variacaoForm.value.variacoes[j].sku) {
console.log('x')
}
}
}