How do I get the method "Object.fromEntries( )" to work?

I’ve been working on the challenges in the Introduction to JavaScript: Advanced Objects - Review. The last challenge asks us to try out other built-in object methods, and I’ve been trying to use Object.fromEntries(), together with Object.entries() and the.map() array manipulation method, in a similar way to what is demonstarated in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries#Object_transformations, because it looked like it might come in useful… not to mention pretty cool :wink:

Here is a simplified example of the sort of thing I’ve been trying to get running:

const points = {
  strength: 9,
  skill: 7,
  intelligence: 5
}

const pointsDoubled = Object.fromEntries(
  Object.entries(points).map(([attribute, score]) => {
    return [attribute, score * 2]
  })
)

console.log(pointsDoubled)

Expected to log:
{strength: 18, skill: 14, intelligence: 10}

Instead logs:
TypeError: Object.fromEntries is not a function at Object.<anonymous>

The problem is definitely with the Object.fromEntries() because when I remove this method…

const pointsDoubled = Object.entries(points).map(([attribute, score]) => {
    return [attribute, score * 2]
  })

console.log(pointsDoubled)

…it logs the expected array:
[['strength', 18], ['skill', 14], ['intelligence', 10]]

Indeed, if I start with this array, and then just try to use Object.fromEntries() on its own to convert the array into an object…

const array = [['strength', 18], ['skill', 14], ['intelligence', 10]]

const obj = Object.fromEntries(array)

console.log(obj)

…it logs the same error message as before:
TypeError: Object.fromEntries is not a function at Object.<anonymous>

Can anyone help? :thinking:

Which browser are you working in? This is still a proposal and may not yet be adopted or supported by all browsers.

Google Chrome, version 74

In the browser compatibility section it says that Object.fromEntries() is supported from version 73

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries#Browser_compatibility

1 Like

Okay, we can rule out lack of support.

Could you please guide us through why this is nesting one method inside the other?

const pointsDoubled = Object.fromEntries(
  Object.entries(points).map(([attribute, score]) => {
    return [attribute, score * 2]
  })
)

Belay, belay. I’ve just read the page in your link.

 > attr_array = [['strength', 18], ['skill', 14], ['intelligence', 10]]
<- (3) [Array(2), Array(2), Array(2)]
 > Object.fromEntries(attr_array)
<- {strength: 18, skill: 14, intelligence: 10}

That’s in Chrome 74.

 > points = {
     strength: 9,
     skill: 7,
     intelligence: 5
   }
<- {strength: 9, skill: 7, intelligence: 5}
 > pointsDoubled = Object.fromEntries(
     Object.entries(points).map(([attribute, score]) => {
       return [attribute, score * 2]
     })
   )
<- {strength: 18, skill: 14, intelligence: 10}

I don’t understand this, and I’m not sure what I should do differently…
When I change my code for…

… I get the same error message.

Or is this aimed at getting someone else’s input? :upside_down_face:

Just a change of name, is all. I ran the code in Chrome console with no problems.

1 Like

mmmm… when I say I’m using Chrome, that’s to access the Codecademy platform. I also tried coding it in Visual Studio Code and running it using node.js…
BUT
Having checked the compatibility info again, I’ve now spotted that Object.fromEntries() isn’t supported in node.js yet, so I guess that’s why it’s not running in either. I’ve also now noticed that in the error message displayed in the Codecademy console there are a couple of references to bootstrap_node.js, so I guess the Codecademy platform uses node.js too…

Sorry, I didn’t realise this earlier… showing my lack experience…

3 Likes

You chased it down, and got some answers. Kudos.

2 Likes