简体   繁体   English

Javascript 中 React 格式的对象和数组

[英]Objects and Arrays in Javascript in React formatting

I have local data where I can access the first item using data{0}.我有本地数据,我可以在其中使用数据访问第一项{0}。 Now I am moving over to a database and I get the data and create another object, but can no longer access the first item using data{0}.现在我正在转移到一个数据库并获取数据并创建另一个对象,但无法再使用数据访问第一个项目{0}。

I feel like I have a data nesting issue of some kind, but can't figure out how to format the data correctly.我觉得我有某种数据嵌套问题,但无法弄清楚如何正确格式化数据。 You can see in both cases I'm definitely getting data:在这两种情况下,您都可以看到我肯定会获取数据:

console.log result console.log 结果

Local Data:本地数据:

module.exports = [
  {
    date: "01/01/1802"
}
]

Remote Database Data:远程数据库数据:

    const databaseResult = web.lists
  .getByTitle("Database-Content")
  .items.get()
  .then((TimelineConent) => {
    TimelineConent.forEach(function (item, index) {
      items.push({
        date: item.date,
       
      });
    });

  });


this.setState({ items: items });

How can I format my database data to match the original local data format?如何格式化我的数据库数据以匹配原始本地数据格式?

Your data is stored as objects in an array, which means that you have to use data[0] instead of data{0} to get the first object from the array.您的数据作为对象存储在数组中,这意味着您必须使用 data[0] 而不是 data{0} 从数组中获取第一个对象。 Then you can access the values of the object using pointers.然后您可以使用指针访问对象的值。

let data = [{value: 2}]
console.log(data[0].value)

Outputs: 2输出: 2

I'm not sure whether you are asking about this or about preventing the nesting at data creation.我不确定您是问这个还是防止数据创建时的嵌套。

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

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