简体   繁体   English

如何在react-select上制作for循环?

[英]How to make for loop on react-select?

I making a select option using react-select following this Installation and usage example code.我在此安装和使用示例代码之后使用 react-select 制作了一个选择选项。

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.现在,我想让它动态地存储来自其他数据的值并使其在对象上迭代。

like this:像这样:

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..?那么,如何在其中进行迭代..?

or should I thinking to figure out in the component..?还是我应该考虑在组件中弄清楚..?

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

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存储数据如下:

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

then if the array store 2 data, then the const options be like this:那么如果数组存储 2 个数据,那么const options是这样的:

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

The purpose of this are for make dropdown options on the select have dynamically values.这样做的目的是使 select 上的下拉选项具有动态值。

You can use an .map function to dynamically created your options :)您可以使用.map函数动态创建您的选项:)

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';


const options = [
    { value: 'flavor', label: 'flavor' },
    { value: 'yummy', label: 'yummy' },
    { value: 'red', label: 'red' },
    { value: 'green', label: 'green' },
    { value: 'yellow', label: 'yellow' },
];

class App extends Component {
    state = {
        selectedOption: 'None',
    }

    handleChange = ({ target }) => {
        this.setState({
            selectedOption: target.value,
        });
    }

    render = () => (
        <div>
            <span>{this.state.selectedOption}</span>
            <select
            value={this.state.selectedOption}
            onChange={this.handleChange}
            >
            {options.map(({ value, label }, index) => <option value={value} >{label}</option>)}
            </select>
        </div>
    );
}

render(<App />, document.getElementById('root'));

Working example here https://stackblitz.com/edit/react-select-example这里的工作示例https://stackblitz.com/edit/react-select-example

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM