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