Reputation: 1319
I want to remove elements from an array only if exist empty elements in another array. I have this:
var fruits = ["Banana", "Naranja", "Manzana", "Mango", "Fresa", "Limón", "Lima", "Sandia"];
var vegetales = ["", "Ajo", "", "Cebolla", "Tomate", "", "", "Zanahoria"];
// The output should be this:
// ["Naranja", "Mango", "Fresa", "Sandia"]
function myFunction() {
for (var i in vegetales) {
if (vegetales[i] == '') {
frutaAeliminar = fruits[i];
indexFruta = fruits.indexOf(frutaAeliminar);
if (indexFruta != -1) {
fruits.splice(indexFruta, 1)
}
}
}
console.log(fruits);
}
myFunction();
Upvotes: 0
Views: 63
Reputation: 386560
You could filter with the truthy value of the corresponding vegetales
item.
var fruits = ["Banana", "Naranja", "Manzana", "Mango", "Fresa", "Limón", "Lima", "Sandia"];
vegetales = ["", "Ajo", "", "Cebolla", "Tomate", "", "", "Zanahoria"],
newArray = fruits.filter((_, i) => vegetales[i]);
console.log(newArray)
Upvotes: 2
Reputation: 23979
Example using ES6
Use Array.filter and using &&
in a ternary allows us to not bother with else
if it's not needed
const fruits = ["Banana", "Naranja", "Manzana", "Mango", "Fresa", "Limón", "Lima", "Sandia"];
const vegetales = ["", "Ajo", "", "Cebolla", "Tomate", "", "", "Zanahoria"];
/* only pick if corresponding array item does not equal '' */
const newArray = fruits.filter((fruit, idx) => (vegetales[idx] !== '' && fruit))
console.log(newArray)
Upvotes: 2
Reputation: 13623
I think you're getting mixed up because you're changing the length of the array while looping through it. This solution uses a third array to push results to w/o changing the original arrays.
Also, a few quick reminders:
for...in
only for iterating over object key/value pairs-- otherwise use a standard for
loop.var
statement --otherwise you're going to pollute the global namespace.var fruits = ["Banana", "Naranja", "Manzana", "Mango", "Fresa", "Limón", "Lima", "Sandia"];
var vegetales = ["", "Ajo", "", "Cebolla", "Tomate", "", "", "Zanahoria"];
var i;
var outputArr = [];
// The output should be this:
// ["Naranja", "Mango", "Fresa", "Sandia"]
function myFunction() {
for (i=0; i<vegetales.length; i++) {
if (vegetales[i] !== '') {
outputArr.push(fruits[i]);
}
}
console.log(outputArr);
}
myFunction();
Upvotes: 1
Reputation: 13651
Simple solution:
var fruits = ["Banana", "Naranja", "Manzana", "Mango", "Fresa", "Limón", "Lima", "Sandia"];
var vegetales = ["", "Ajo", "", "Cebolla", "Tomate", "", "", "Zanahoria"];
var result = [];
for(var i = 0;i<fruits.length;i++){
if(vegetales[i]!=""){
result.push(fruits[i]);
}
}
console.log(result);
Output:
["Naranja", "Mango", "Fresa", "Sandia"]
Run here:
var fruits = ["Banana", "Naranja", "Manzana", "Mango", "Fresa", "Limón", "Lima", "Sandia"];
var vegetales = ["", "Ajo", "", "Cebolla", "Tomate", "", "", "Zanahoria"];
var result = [];
for(var i = 0;i<fruits.length;i++){
if(vegetales[i]!=""){
result.push(fruits[i]);
}
}
console.log(result);
N.B.: Modification can be made.
Upvotes: 1