Sign in
Log inSign up

Form a new object from existing data

Emil Moe's photo
Emil Moe
·Dec 24, 2017

I'm trying to form a new object in JavaScript based on existing data. I thought it would be a simple task but somehow not.

I want to define the key by year_id and I think this is where it complicates.

Here's my snippet. I tried more pretty solutions as .reduce() but it didn't work so this was an attempt to make it very simple, but it still doesn't work. The resulting object (titles) is just a list of year_id with the values null.

let titles = {}

this.user.titles.forEach(title => {
    let key = title.year_id

    titles[key] = {}
    titles[key].id = title.title.id
    titles[key].en = title.title.en
})

return titles
Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct