Define a mobx type for an unkown key?

728 Views Asked by At

Currently have a data set with an object of arrays where each array has it's unique id as a key for reference.

"relations": {
    "19271934": [
        19271894
    ],
    "19621318": [
        19621538,
        19621586
    ],
    "19788661": [
        19788191
    ],
    "19788662": [
        19788192
    ]
}

Would like to create a model for an individual relation so it would be something like this.

relations: types.Map(types.model({
    relationId: types.optional(types.array(RelationModel),[]) 
})),

where the relationId would be the unique id I have no way for knowing before.

1

There are 1 best solutions below

0
On BEST ANSWER

You can define it as a map of arrays that contain numbers:

const Store = types.model({
  relations: types.map(types.array(types.number))
});

const store = Store.create({
  relations: {
    "19271934": [19271894],
    "19621318": [19621538, 19621586],
    "19788661": [19788191],
    "19788662": [19788192]
  }
});