Search code examples
javascriptreduximmutable.js

ImmutableJS programatic key naming?


I want to be able to create a Map and programmatically name the index. is
this impossible because Javascript is dynamically typed and Map needs a string?

for more context in case I am missing a better pattern: i get a bunch of person objects (name, id) from server, and now initialize a profile Map.

export function makeMap(person) {

 const profileMap = Map({
   person.id: Map({
     id: person.id,
     name: person.name,
     foo: false,
     otherthing: 5,
     favorites: Map({}),
   })
 })

Solution

  • Not totally sure what you're asking but if you want to use the value of the persons id as a key in an object it would look like:

    const { id, name } = person
    const profile = {
      [id]: {
        id,
        name,
        foo: false,
        otherthing: 5,
        favorites: {}
    }