lukehaas/RunJS

unexpected result

Closed this issue · 2 comments

let obj = {
name:"Rajeev",
place: "Perth",
job: "Graphic Designer",
title: "Web Developer"
}
let newObj = Object.entries(obj)

newObj.map((ent, i)=>{
console.log(ent, i)

})

OUTPUT BELOW

[ 'name', 'Rajeev' ] 0
[ 'place', 'Perth' ] 1
[ undefined, undefined, undefined, undefined ]
[ 'job', 'Graphic Designer' ] 2
[ 'title', 'Web Developer' ] 3

It is confusing, why there is an array with undefined ????,

It's because the function in the map is not returning anything.

newObj.map((ent, i)=>{
console.log(ent, i)

})

It creates a new array where each item is undefined. If this is not what you want, then you should change it to a forEach.

@heatcoder closing this now. Hope the explanation was clear.