Search code examples
javascriptarraysiteratorset

Getting Undefined error at the end of sets Iteration in JavaScript


I am getting an undefined error after logging each element of the set. please have a look at the code snippet bellow

  let arrayFruits = ['apple', 'banana', 'orange', 'plum', 'peach', 'strawberry', 'raspberry'];

const fruits = new Set(arrayFruits);

const itrate = fruits.forEach(fruit =>{
    console.log(fruit);

});

console.log(itrate);

Solution

  • what you probably need

    let arrayFruits = ['apple', 'banana', 'orange', 'plum', 'peach', 'strawberry', 'raspberry'];
        
    const fruits = new Set(arrayFruits);
        
    let itrate = []
    fruits.forEach(fruit =>{
      itrate.push(fruit)
    });
                
    console.log(itrate);