简体   繁体   English

你如何在 React 中映射 json?

[英]How do you map json in React?

I'm trying to show the following Json in a table using react, but I'm not sure how I can access the elements that are inside the JsonArray of empresas , Bodega_Origen y Bodega_Destino .我正在尝试使用 react 在表中显示以下 Json,但我不确定如何访问empresasBodega_Origen y Bodega_Destino 的 JsonArray 中的元素

  {
        "Estado": 1,
        "_id": "5e31ecf510ec6117387548a1",
        "usuarios": "5e262750803c451ca4a27c36",
        "empresas": {
            "_id": "5e2f4c3ef80d8246f0e545d8",
            "Nombre_Empresa": "ING. Y HER. S.A."
        },
        "Tipo_Documento": "AF",
        "Numero_Documento": "1",
        "Bodega_Origen": {
            "_id": "5e2619ccbb29d71aa8455cad",
            "Descripcion": "BODEGA PRINCIPAL"
        },
        "Bodega_Destino": {
            "_id": "5e2629f110b9f32d10524ca5",
            "Descripcion": "BODEGA Obra parma"
        },
        "Responsable": "Mauricio",
        "Estado_Documento": "PR",
        "Observaciones": "",
        "Detalles": [],
        "Fecha_Creacion": "2020-01-29T20:37:09.919Z"
    }

The following is the method I am using to obtain the data and parse it.以下是我用来获取数据并解析它的方法。 You are currently browsing the data correctly, but it does not show the data that is within the Bodega_Origen y la Bodega_Destino.您当前正在正确浏览数据,但它没有显示 Bodega_Origen y la Bodega_Destino 中的数据。

  loadData(){
        var cont = 0;
        console.log('Getting list');
        const activeTab = this.state.activeTab;
        const ids = this.ids;
        const path = activeTab===0?'/empresa/list':'/articulo/list';
        console.log(this.state.listQuery);
        cont++;
        console.log(cont)
        return this.Requests.list(path, this.state.listQuery).then(response=>{
            if(response && response.length !== 0){// El request no ha devuelto un arreglo vacio
                let dt = response;
                dt.map((el,i)=>{
                    let filterProperties = [];//Propiedades de interes
                    filterProperties[i] = {};//Inicializar el objeto para cada elemento del arreglo
                    ids[activeTab].forEach(key=>{
                        if(el[key]._id){
                            el[key] = el[key].nombre;
                        }
                        filterProperties[i][key] = el[key];
                    })
                    return filterProperties;
                });
                console.log(dt);
                let stateData = this.state.data.slice();
                stateData[activeTab] = dt.slice();
                this.setState({data:stateData});
            }else{
                let stateData = this.state.data.slice();
                stateData[activeTab] = [];
                this.setState({data:stateData})
            }
        });
    }

Per se an Object is not iterable.对象本身是不可迭代的。 You cannot map an Object like an Array in React.你不能像 React 中的数组那样映射对象。 You can try it with Object.entries(obj) and then loop over those key, value pairs and get what you want.您可以尝试使用Object.entries(obj) ,然后遍历这些键、值对并获得您想要的。

To access that json data you will need to specify it:要访问该 json 数据,您需要指定它:

 data.empresas data.Bodega_Origen data.Bodega_Destino
In that way you access the nested json object. 通过这种方式,您可以访问嵌套的 json 对象。 JSON is an object type, not an array. JSON 是一种对象类型,而不是数组。 Hope this helps :) 希望这可以帮助 :)

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

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