1

My data structure is not strictly an array, it is more like an array inside of a nested object.

{
    "start": "3-1-2021",
    "event_log": [
        {
            "event_id": 1,
            "rul": 61300,
            "start": "3-1-2021"
        },
        {
            "event_id": 2,
            "rul": 61299,
            "start": "3-1-2021"
        },
    ]
}

How can I extract the event_id as the entity and pass it into the createEntityAdapter. I have tried this:

const chartAdapter = createEntityAdapter({
    selectId: (data) => data.event_log.map(item => item.event_id)
});

But it is not working. Anyone has an idea of how to ignore the start and destructure the event_id from event_log and pass it into the createEntityAdapter. Much appreciated.

David Ho
  • 207
  • 4
  • 13

1 Answers1

0
const EventLogAdapter = createEntityAdapter<EventLog>({
  selectId: log => log.event_id,
})

type State = Partial<{
  start: string
  // other fields
}> & {
  eventLogs: EntityState<EventLogAdapter>
}

const initialState: State = {
  eventLogs: EventLogAdapter.getInitialState(),
}

const LogsSlice = createSlice({
  name: 'logs',
  initialState,
  reducers: {
    // reducers
  },
})

Similar answer as this

Snainer
  • 186
  • 1
  • 7