如何在react-select上进行for循环? [英] How to make for loop on react-select?

查看:879
本文介绍了如何在react-select上进行for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此安装和使用示例代码之后,使用react-select进行选择。

I making a select option using react-select following this Installation and usage example code.

有一个数组对象,用于存储这样的选项:

There was an object of array to store the option like this:

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
];

现在,我想使它动态地存储其他数据的值并使其在

Now, I want to make it dynamically to store the value from other data and make it iterate on the object.

像这样:

const options = [
      { value: flavor, label: flavor },
    ];

值和标签上的风味是一个数组。
假设数组存储的数据如下:

The flavor on the value and the label are an array. Let's say the array was store the data like this:

flavor = ['chocolate', 'strawberry', 'vanilla']

因此,每当数组添加新值时,上面的对象也会添加

So, whenever the array added new value, the object of array above also add the value on the iterate.

所以,如何在其中进行迭代..?

So, how to making iterate in that..?

<Select
  value={selectedOption}
  onChange={this.handleChange}
  options={options}
/>

编辑:我想要的结果是数组内的对象是根据数组值添加的值,假设数组中有一个数据,则 const选项存储的数据如下:

EDIT: the result what I want are the object inside the array is added according to the array values, let's say if we have one data inside array, then the const options store data like this:

const options = [
      { value: flavor, label: flavor },
    ];

然后,如果数组存储2个数据,则 const选项像这样:

then if the array store 2 data, then the const options be like this:

const options = [
      { value: flavor, label: flavor },
      { value: flavor, label: flavor },
    ];

此目的是为了使select上的make下拉选项具有动态值。

The purpose of this are for make dropdown options on the select have dynamically values.

推荐答案

我通过以下此答案,这是我根据自己的问题想要得到的答案:

I have figure out this with following this answer, this is the answer what I want base on my questions:

const options = [];
    obj = {}

    for(var i = 0; i < flavor.length; i++) {
      var obj = {};

      obj['value'] = flavor[i];
      obj['label'] = flavor[i];
      options.push(obj);
    }

这篇关于如何在react-select上进行for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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