ID保存和显示名称-带有芯片的材料ui多选 [英] ID Save and Display Name - material ui multiselect with chips

查看:77
本文介绍了ID保存和显示名称-带有芯片的材料ui多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据文档示例使用Material UI Multiple Select.我需要保存所选选项的ID并显示名称, 我尝试了很多方法,但没有任何帮助. 我需要存储id并显示值.请帮我指导.

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.

这是我的密码框链接

推荐答案

我一直在想同样的事情,而我发现使它起作用的唯一方法是将array转换为object.

I was wondering about the same and the only way I found to make it work is by transforming the array to an object.

此处是沙箱: https://codesandbox.io/s/material-demo- 5cy6p

在其中,您可以看到names不再是array而是一个菜单选项值为key且要实际显示为value的名称的对象:

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',
}

通过这种方式,您可以在O(1)时间内访问值(名称)(如果您不熟悉Big O表示法,请阅读以下内容:

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.

这篇关于ID保存和显示名称-带有芯片的材料ui多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆