繁体   English   中英

在javascript中访问类型为object的数组成员

[英]access array member of type object in javascript

我一直在尝试从数组(data_array)访问数组成员。我的变量data_array是json的一部分,类似于下面的代码。 下面的代码不是实际的代码。 当我尝试创建React组件时,实际的代码正在发挥作用。 我可以粘贴完整的React组件,但是它将包含一些不必要的信息。 所以我想我会添加一些模拟的例子。 道歉,如果它误导了人们。 目前,我希望对这种情况下可能出什么问题获得一些提示?

data: {
    "name": "Arjun",
    "records" : [
      {
        "value": 3
      },
      {
        "value": 6
      },
      {
        "value":7
      }
    ]
  }
   var data_array = data.records

   console.log(data_array) -> Array[3]
   console.log(data_array[0]) -> It displays Uncaught TypeError: Cannot read property '0' of undefined
   console.log(typeof data_array) -> Object. 

让我感到困惑的是我的第一个输出,因为它说它是一个Array。 所以也许我缺少了一些东西。 任何帮助,将不胜感激。

编辑:这仍然会缺少一个嵌套的React组件,但是在这种情况下将没有用。 我现在将嵌套组件称为Foo


var React = require('react'),
  Router = require('react-router'),
  mui = require('material-ui'),
  Foo = require('./foo.jsx');


var TempReact = React.createClass({
  mixins: [Router.Navigation],

  getInitialState: function() {
    return {
      data: {}
    };
  },

  componentDidMount: function() {
    // Do a web request here to actually get the data from the backend API
    // this.setState({

    // });

    // For now, load our fake data
    this._loadFakeData();
  },

  render: function() {
    //console.log(this.state.data)
    for(var record in this.state.data.records) {
      console.log(record.Value);
    }
    //console.log(this.state.data["records"][0]["Value"])
    return (
      <div>

        <p>^ That will still say , since the header up there is decided by temp-page.jsx, which is
          based on the route</p>

        <br/>
        <p>We've loaded the fake data into our component's state, and here's proof:</p>
        {this.state.data.name}
        <br/>
        <br/>

        <Foo name={["temp1",this.state.data.records,"green"]}/>

      </div>
    );
  },

  _loadFakeData: function() {
    this.setState({
    data: {
        "name": "Arjun",

        "records" : [
          {
            "Value": 6
          },
          {
            "Value": 7
          },
          {
            "Value": 8
          }
        ]
      }
    });
  }

});

module.exports = TempReact;

数组是typeof对象。 它们只是一个带有一些奇特事物的对象。

以下示例起作用:

var data = {
"name": "Arjun",
"records" : [
    {
        "value": 3
    },
    {
        "value": 6
    },
    {
        "value":7
    }
  ]
}
var data_array = data.records
console.log(data_array[0]) // Object {value: 3}

当您执行以下操作时,将发生此错误:

var data = {};
var data_array = data.array; // data.array is undefined and subsequently data_array.
console.log(data_array[0]); // Throws: Uncaught TypeError: Cannot read property '0' of undefined

距离您不远,可以说您仅将此示例存储在名为jsonData的变量中:

var jsonData = {
  data: {
    "name": "Arjun",
    "records" : [
      {
        "value": 3
      },
      {
        "value": 6
      },
      {
        "value":7
      }
    ]
  }

}

您需要访问变量> property> nested property,然后像这样指定索引:

var data_array = jsonData.data;
   console.log(data_array.records[0].value)

->应该记录3

此处的示例: http : //codepen.io/theConstructor/pen/wGdpjq

因此问题可能出在React中的SetState()上,我能够通过首先将它们推入一个空数组来访问这些对象:

var varArr = new Array();
for(key in this.state.data.records) {
   if(this.state.data.records.hasOwnProperty(key)) {
       var value = this.state.data.records[key];
       varArr.push(value);
   }
 }

data_array是一个数组。 data.records在一个数组中。 您希望看到什么?

暂无
暂无

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

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