如何从对象数组中获取特定数据?

时间:2023-02-09 09:02:31

I'm a beginner and would like to know how I can get a specific object from an array

我是初学者,想知道如何从数组中获取特定对象

I have an Array that looks like this:

我有一个看起来像这样的数组:

data {
    "orderid": 5,
    "orderdate": "testurl.com",
    "username": "chris",
    "email": "",
    "userinfo": [
      {
        "status": "processing",
        "duedate": "" ,
      }
    ]
  },

To get the data from above I would do something like this:

要从上面获取数据,我会做这样的事情:

return this.data.orderid

But how can I go deeper and get the status in userinfo?

但是,如何更深入地获取userinfo中的状态?

 return this.data.orderid.userinfo.status

doesn't work... anyone have any ideas?

不起作用......任何人都有任何想法?

4 个解决方案

#1


2  

A few points:

几点:

  • data is not an array, is an Object (see the curly braces, arrays have squared brackets). To be really precise, your syntax is invalid, but I assume you wanted to type data = { ... }, as opposed to data { ... }
  • 数据不是数组,是一个Object(参见花括号,数组有方括号)。准确地说,你的语法是无效的,但我想你想输入data = {...},而不是数据{...}

  • Your syntax is almost correct, the only mistake you are making is that userinfo is an array, and arrays have numeric indexes (I.e. array[0], array[1]). What you are looking for is this.data.orderid.userinfo[0].status
  • 你的语法几乎是正确的,你唯一的错误是userinfo是一个数组,而数组有数字索引(即数组[0],数组[1])。你要找的是this.data.orderid.userinfo [0] .status

#2


1  

Use data.userinfo[0].status to get the value (in your case this.data.userinfo[0].status)

使用data.userinfo [0] .status获取值(在您的情况下为this.data.userinfo [0] .status)

var data = {
    "orderid": 5,
    "orderdate": "testurl.com",
    "username": "chris",
    "email": "",
    "userinfo": [
      {
        "status": "processing",
        "duedate": "" ,
      }
    ]
  };
  console.log(data.userinfo[0].status);

#3


0  

User Info is an array, so you would need to access it using indexer like so:

用户信息是一个数组,因此您需要使用索引器来访问它,如下所示:

return this.data.userinfo[0].status

MDN on arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

阵列上的MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

#4


0  

You need to iterate over data.userinfo (it's an array)

你需要迭代data.userinfo(它是一个数组)

var data = {
    "orderid": 5,
    "orderdate": "testurl.com",
    "username": "chris",
    "email": "",
    "userinfo": [
      {
        "status": "processing",
        "duedate": "" ,
      }
    ]
  };
  
data.userinfo.forEach(function(element) {
  console.log(element.status);
});

#1


2  

A few points:

几点:

  • data is not an array, is an Object (see the curly braces, arrays have squared brackets). To be really precise, your syntax is invalid, but I assume you wanted to type data = { ... }, as opposed to data { ... }
  • 数据不是数组,是一个Object(参见花括号,数组有方括号)。准确地说,你的语法是无效的,但我想你想输入data = {...},而不是数据{...}

  • Your syntax is almost correct, the only mistake you are making is that userinfo is an array, and arrays have numeric indexes (I.e. array[0], array[1]). What you are looking for is this.data.orderid.userinfo[0].status
  • 你的语法几乎是正确的,你唯一的错误是userinfo是一个数组,而数组有数字索引(即数组[0],数组[1])。你要找的是this.data.orderid.userinfo [0] .status

#2


1  

Use data.userinfo[0].status to get the value (in your case this.data.userinfo[0].status)

使用data.userinfo [0] .status获取值(在您的情况下为this.data.userinfo [0] .status)

var data = {
    "orderid": 5,
    "orderdate": "testurl.com",
    "username": "chris",
    "email": "",
    "userinfo": [
      {
        "status": "processing",
        "duedate": "" ,
      }
    ]
  };
  console.log(data.userinfo[0].status);

#3


0  

User Info is an array, so you would need to access it using indexer like so:

用户信息是一个数组,因此您需要使用索引器来访问它,如下所示:

return this.data.userinfo[0].status

MDN on arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

阵列上的MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

#4


0  

You need to iterate over data.userinfo (it's an array)

你需要迭代data.userinfo(它是一个数组)

var data = {
    "orderid": 5,
    "orderdate": "testurl.com",
    "username": "chris",
    "email": "",
    "userinfo": [
      {
        "status": "processing",
        "duedate": "" ,
      }
    ]
  };
  
data.userinfo.forEach(function(element) {
  console.log(element.status);
});