lukehaas / RunJS

RunJS is a JavaScript playground for macOS, Windows and Linux. Write code with instant feedback and access to Node.js and browser APIs.
https://runjs.app
2.01k stars 43 forks source link

unexpected result #571

Closed heatcoder closed 10 months ago

heatcoder commented 11 months ago

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 ????,

lukehaas commented 11 months ago

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.

lukehaas commented 10 months ago

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