I can't seem to find the bug that's making the code run twice, and JSFiddle isn't working for me so I can't double check if it's the editor.
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: 9,
address: ["5242", "drank avenue"]
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: 8,
address: ["3368", "pool lane"]
};
var list = function(person)
{
for (var friendName in person)
console.log(friendName);
};
list(friends);
Output:
bill
steve
bill
steve
I cut and pasted the code you posted into my JavaScript console and the result was:
bill
steve
Somewhere, in your original code, you must be calling the function twice.
Choose a place in the code — in this case I would choose the top of your list
function — and put a
debugger;
statement there, and open the JavaScript console.
There will be an option in that debugger to see a stack trace. The stack trace will let you see exactly where a function was called from, so you can see if it is being called from two different points.
If that doesn't work, move the debugger statement to right above this line:
friends.bill = {
and repeat until you find the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With