简体   繁体   English

在 React 中访问嵌套的 JSON 数据

[英]Accessing nested JSON data in React

Link to app: https://boiling-coast-12353.herokuapp.com/应用链接: https : //boiling-coast-12353.herokuapp.com/

Bear in mind this is for an assignment that does not require components yet (but will) What I have so far works, but now I would like to transform my data by using a nested JSON.请记住,这是针对尚不需要组件的作业(但会)到目前为止我所拥有的工作,但现在我想通过使用嵌套的 JSON 来转换我的数据。 Currently so I have JSON data that looks like this:目前,我有如下所示的 JSON 数据:

[
    { "label": "Argentina", "percentage": 10.44 },
    { "label": "Bolivia", "percentage": 51.62 },
     etc..
]

which I access to generate buttons and generates divs for each object in the array respectively:我访问它以分别为数组中的每个对象生成按钮和生成 div:

{
  this.state.data.map((data, index) => (
  <button key={index}className="button-primary" onClick={() => 
  {this.onChooseCountry(index);}}>
     <span className="btn-content" tabindex="-1">
     </span>{data.label}</button>
  ))
} 

{
  this.state.chosenCountries.map((chosenCountry, index) => (
  <div className="bar--show bar" style={{height: chosenCountry.percentage + "%"}}>

  <h3>{chosenCountry.label}</h3>
  <h4>{chosenCountry.percentage} %</h4>
  <button className="remove-btn" onClick={() => this.removeCountry(index)}>remove</button>

 </div>
   ))
 }

The new data set I'd like to work with I've organized like this:我想使用的新数据集是这样组织的:

[
    { 
    "year": "2010",
     "info": [
        { "Country": "Argentina", "Percentage": 10.44 },
        { "Country": "Bolivia", "Percentage": 51.62 },
          ...
    ]
    },
    {
    "year":"2011",
    "info": [
        { "Country": "Argentina", "Percentage": 10.34 },
        { "Country": "Bolivia", "Percentage": 51.62 },
    ...

And I would like to pass the year/label for array into a select drop down menu:我想将数组的年份/标签传递到选择下拉菜单中:

<select onChange={this.updateYear} id="select" className="YearChooser-select">
   <option value="">Select Year</option>
   <option value="2016">2016</option>
   <option value="2015">2015</option>
   <option value="2014">2014</option>
   ...

</select>

Method Im using so far that gets the year value from the dropdown:到目前为止,我使用的方法是从下拉列表中获取年份值:

updateYear = (ev) => {

    this.setState({
      year: ev.target.value },
      () => console.log('update year', this.state.year));

  }

I figured out how to access the new nested data set using sub, but I don't know how to link the selected year from the drop down menu to the info for each year:我想出了如何使用 sub 访问新的嵌套数据集,但我不知道如何将下拉菜单中的选定年份链接到每年的信息:

{
  this.state.data.map((datum) =>
  <div>
    {datum.year}
  <ul>
    {datum.info.map((sub) =>
    <div>
      {sub.Country} : 
       {sub.Percentage} %
    </div>
    )}
  </ul>
  </div>
  )
}
const options = this.state.data.map((datum) => (
    <option value={datum.year}>{datum.year}</option>
);

<select onChange={this.updateYear} id="select" className="YearChooser-select">
   {options}
</select>

is this what you looking for?这是你要找的吗?

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

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