通过react-select仅使用ID数组作为值

时间:2019-09-25 10:27:28

标签: reactjs

我想在像这样处理数据数组的同时使用像[2,4]这样的id数组进行反应选择:

[
    { value:1, label:"item1" },
    { value:2, label:"item2" },
    { value:3, label:"item3" },
    { value:4, label:"item4" },
]

我在文档中找不到如何自定义这些内容。

1 个答案:

答案 0 :(得分:2)

您可以使用map循环为react-select创建选项。

const datas =  [2,4];
datas.map((d) => {return { value: d, label: d }})

演示:https://codesandbox.io/s/react-select-0tdlq