简体   繁体   中英

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

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) 

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) 

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

NB: Modification can be made.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM