ID Save and Display Name - material ui multiselect with chips

末鹿安然 提交于 2020-02-25 23:51:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!