使用reactjs拆分数组值

时间:2018-01-31 06:23:59

标签: javascript reactjs

我有一个这样的数组:

+---+----+------+
| id|name|charge|
+---+----+------+
| 22|  aa|    40|
| 22|  bb|    40|
| 11|  mm|    20|
+---+----+------+

我想将该数组放入反应中。所以我期待这样的结果

["Daniel", "Adam", "Charlie", "Brad"]

我怎么解决?任何答案都将不胜感激。

2 个答案:

答案 0 :(得分:1)

你可以在阵列上使用map

let a = ["Daniel", "Adam", "Charlie", "Brad"];
<select>
    {
        a.map((item , index) => <option key={index}> {item} </option>)
    }
</select>

答案 1 :(得分:0)

这很简单。仅map

array

class App extends React.Component {

      constructor() {
        super();
        this.state = {
          arr: ["Daniel", "Adam", "Charlie", "Brad"]
        }
      }


      render() {
      let {arr} = this.state;
        return (
          <select>
          {arr.map((x, i) => <option key={i}>{x}</option>)}
          </select>
        )
      }

    }

    ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>