简体   繁体   中英

How to get specific data from object array?

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?

 return this.data.orderid.userinfo.status

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

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 { ... }
  • Your syntax is almost correct, the only mistake you are making is that userinfo is an array, and arrays have numeric indexes (Ie array[0] , array[1] ). What you are looking for is this.data.orderid.userinfo[0].status

Use data.userinfo[0].status to get the value (in your case 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); 

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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