0

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();

vatz88
2,4522 gold badges17 silver badges27 bronze badges
asked May 7, 2017 at 18:40

4 Answers 4

2

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)

answered May 7, 2017 at 18:50
Sign up to request clarification or add additional context in comments.

Comments

2

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)

answered May 7, 2017 at 18:58

1 Comment

Slick - reduced me even more!
1

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();

answered May 7, 2017 at 18:47

Comments

1

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.

answered May 7, 2017 at 18:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.