After delving into the world of JavaScript for about six months now, I've come across something that has left me puzzled. This is completely new to me, so I'm not entirely sure what's happening here. I am attempting to create a new object within a for loop of another object. My new object, named value
, should have the same key name as the one in the current loop; however, all I'm getting back is 'key' instead. Strangely enough, when I simply log it out to the console, it shows the correct key name that I wanted. What on earth could be causing this confusion? There must be some reason behind this phenomenon that I'm yet to discover.
const testObj = (something => {
for (let key in something) {
let value = {key: something[key]};
console.log(key);
console.log(value);
}
})
let test = {'name': 'yomam', 'address': 'camelbak'};
console.log(testObj(test));
/*
name
{ key: 'yomam' }
address
{ key: 'camelbak' }
*/