Carmoreno
Carmoreno

Reputation: 1319

Remove elements from an array according to empty elements of another array

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

Answers (4)

Nina Scholz
Nina Scholz

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

StudioTime
StudioTime

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

Alexander Nied
Alexander Nied

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:

  1. Use for...in only for iterating over object key/value pairs-- otherwise use a standard for loop.
  2. Don't forget to declare your variables w/ a 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

arshovon
arshovon

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

Related Questions