问题
I am using Material UI Multiple Select based on documentation example. I need to save id of the selected option and show name, I have tried many ways but nothing helps. I need to store the id and Display the value. Please help me with your guidance.
Here is my codesandbox link
回答1:
I was wondering about the same and the only way I found to make it work is by transforming the array
to an object
.
Here goes the sandbox: https://codesandbox.io/s/material-demo-5cy6p
In it you can see how names
is no longer an array
but an object with the value of the menu option as a key
and the name you want to actual show as the value
:
const names = {
1: 'Lorem',
2: 'Ipsum',
}
This way you can access the value(the name) in O(1)
time (if you are not familiar with the Big O notation gives this a read: https://en.wikipedia.org/wiki/Big_O_notation), this is way faster than going through the whole array looking for an object with the same id as the one you are mapping.
来源:https://stackoverflow.com/questions/59942512/id-save-and-display-name-material-ui-multiselect-with-chips