Possible duplicates:
Above possible duplicates are not satisfy my need.
Below is the redux reducer code i used to store as array of objects:
case 'ADD_ITEM':
return {...state, elements:[...state.elements, action.appElements]}
where action.appElements
contains:
{id: '9aq05d', width: '100',height: '225'}
Stored array of objects will look like:
elements: {
0: {
id: 9aq05d,
width: '100',
height: '225',
}
1: {
id: 8lk65f,
width: '200',
height: '787',
}
}
But i need to store the values as key value pair as given below:
Where i need id as key.
elements: {
9aq05d: {
id: 9aq05d,
width: '100',
height: '225',
}
8lk65f: {
id: 8lk65f,
width: '200',
height: '787',
}
}
How to store this kind of key value pair in redux store..?
Thanks in Advance..
Use object spread instead of array spread.
case 'ADD_ITEM':
return {
...state,
elements: {
...state.elements,
[action.appElements.id]: action.appElements
}
}
But keep in mind that the order of keys in objects is not guaranteed.