Please can someone explain what the issue is with my code. What I am trying to achieve is to create an array from fruits array (see code block), where array=[name: "Banana", name: "Orange", name: "Apple", name: "Mango"]
var fruits = ["Banana", "Orange", "Apple", "Mango"];
myFunction(fruits)
function myFunction() {
var array=[];
var item = {};
fruits.forEach(function(entry, index){
item.name=entry
array.push(item);
});
console.log(array)
}
However when I print array to the console, this is what I get: [[object Object] { name: "Mango" }, [circular object Object], [circular object Object], [circular object Object]]
The first object gets populated correctly, but I don't understand why it is saying there is a circular dependancy for the others. Can anyone please help me to explain what the issue is and how I should resolve it. Thanks!