My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

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