I have 2 array one that has "Products" and another that has "productsExcluded".
I have to exclude the products that are in the "productsExcluded" array from the final list "Products" that is shown in a<option>
for(let i=0;i<$scope.productos.length;i++){
$('#select-servicio').append($('<option value='+ $scope.productos[i].idProducto +' >' + $scope.productos[i].descripcion + '</option>'));
}
My idea was to go through them with 2 nested fors, but the array has more than 100,000 values, so it will be super inefficient.
Use the method
includes()
inside the for so you only have one, as followsin the part of
console.log(${element})
it you can replace it with a code that removes the excluded products from the products arrayHow are you?
Making use of
includes()
,filter
andspread operator
, you can achieve the filter, joining the products to be displayed in a select as follows:HTML
JS
Spreat operator converts 2 arrays into a new one with the values that filter() is responsible for returning, together with includes(), which verifies that the excluded array returns products that are not in the products array to generate the new array. Then the results are only iterated by inserting them into the
select>
Cheers!