jeudi 13 mai 2021

filtering array of items using filter method

const fruits = [
  'strawberries',
  'apple',
  'avocados',
  'apple',
  'banana',
  'grapes',
  'apple',
];

function selectFruitBasket(fruitBasket, takenOutFruits) {
  const sanitized = fruitBasket.map((fruit) => {
    return fruit;
  });
  const filterFruit = takenOutFruits
    .filter((sanitize) => {
      if (sanitize === 'apple') {
        return false;
      } else {
        return true;
      }
    })
    .join(',');

  const bothResultFiltering = sanitized + filterFruit;

  return bothResultFiltering
}

console.log(selectFruitBasket(fruits, 'apple'));
//     strawberries, avocados, banana, grapes

console.log(selectFruitBasket(fruits, 'avocados'));
//    strawberries, apple, apple, banana, grapes, apple

Hey, In this function, I'm trying to use the map method to go through the array of items and then use the filter method to filter the array as you see in the expected result, but I get an error that the takenOutFruits does not function so any help to get solution for this issue to filter the array as expected.

Aucun commentaire:

Enregistrer un commentaire