循环通过react js文件中的json对象

时间:2017-11-23 04:32:57

标签: json reactjs create-react-app

我的json对象数组为

 {
  "_index": [
    "houseid"
  ], 
  "added": [
    {
      "description": "detail about column", 
      "houseid": "la 123", 
      "\ufeffID": "154"
    }, 
    {
      "description": "detail about column", 
      "houseid": "la 123", 
      "\ufeffID": "154"
    }, 
    {
      "description": "detail about column", 
      "houseid": "la 123", 
      "\ufeffID": "154"
    }, 
    {
      "description": "detail about column", 
      "houseid": "la 123", 
      "\ufeffID": "154"
    }
  ], 
  "changed": [], 
  "removed": [
    {
      "description": "detail about column", 
      "houseid": "la 123", 
      "\ufeffID": "154"
    }, 
    {
      "description": "detail about column", 
      "houseid": "la 123", 
      "\ufeffID": "154"
    }, 
    {
      "description": "detail about column", 
      "houseid": "la 123", 
      "\ufeffID": "154"
    }, 
    {
      "description": "detail about column", 
      "houseid": "la 123", 
      "\ufeffID": "154"
    }
  ]
}

我想应用一个循环,以便我可以提供添加的详细信息,删除更改如下:        描述houseid effid        关于列la 123 154的细节        关于列la 123 154的详细信息

2 个答案:

答案 0 :(得分:0)

只需输入一些名字,然后得到你想要的东西。

import data from '../../result/result.json';

/* somewhere */
let to-add = data.toadd; // get field what you want.

答案 1 :(得分:0)

您可能希望使用forEach库的lodash方法。它也可以让你自由地遍历对象的键。

_.forEach(object, (value, key) => {
    //do operations on value and key
})

如果你需要通过循环遍历元素来渲染,你可能想要这样做:

render(){
    return (
        <div>
        {_.map(elements, (item) => {
            <div>{item.name}</div>
        })}
        </div>
    );
}

您也可以为表格工作。您也可以参考this回答。