javascript 使用javascript获取对象内数组内对象的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32445211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:17:19  来源:igfitidea点击:

get value of object inside array inside object with javascript

javascriptarrays

提问by Diogo Almeida

If I have this:

如果我有这个:

{
  "data": [{
    "id": "4831",
    "name": "Rui Vasco Martins",
    "nickname": "rvgmartins",
    "money_start": "10200",
    "balance": "8000"
  }]
}

How do I get the value of data->id, data->name, etc ?

我如何获得的价值data->iddata->name等等?

回答by Bene

Data is multiple. It means it is an array of objects. There's no such things as data->id, only data[ n ]->id. In that case, you might want all possible IDs and you can do it with a traditionnal for loop, or other.

数据是多重的。这意味着它是一个对象数组。没有data->id这样的东西,只有data[n]->id。在这种情况下,您可能需要所有可能的 ID,并且可以使用传统的 for 循环或其他方法来实现。

var i =0;
var total = object.data.length;
var ids = [];
for(; i < total; i++) {
   ids.push( data[ i ].id );
}
console.log(ids);

If you are SURE there's only 1 data entry and will always be, you can be straight forward and go object.data[ 0 ].idas well!

如果您确定只有 1 个数据条目并且将永远是,那么您也可以直接前进object.data[ 0 ].id

Please note that "object" is the variable containing the JSON :)

请注意,“对象”是包含 JSON 的变量 :)

And for next time, here's a little hint : http://jsonlint.com/

下一次,这里有一个小提示:http: //jsonlint.com/

Hope that helps!

希望有帮助!

回答by Data

Your object contains an array, so you need to use the correct indices to further delve into the object; your id and name happen to be in the first array, so we use obj.data[0].

您的对象包含一个数组,因此您需要使用正确的索引来进一步深入研究该对象;您的 id 和 name 恰好在第一个数组中,因此我们使用obj.data[0].

var obj = {"data":[{"id":"4831","name":"Rui Vasco Martins","nickname":"rvgmartins","money_start":"10200","balance":"8000","profit":"5010","nreload":"1","deal_id":"4813","created_at":"2015-05-27 12:54:33","updated_at":"2015-07-21 22:31:47"}],"status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"services/dashboard","headers":{"Accept":"application/json, text/pla`in, */*"}},"statusText":"OK"}

You can use either dot or square bracket notation, to further identify the object property, here's dot notation:

您可以使用点或方括号表示法来进一步标识对象属性,这里是点表示法:

var data_id = obj.data[0].id; // "4831"
var data_name = obj.data[0].name; // "Rui Vasco Martins"

Here's square-bracket notation:

这是方括号表示法:

var data_id = obj.data[0]['id']; // "4831"
var data_name = obj.data[0]['name']; // "Rui Vasco Martins"

回答by Moishe Lipsker

As within datathere is an array, you can do:

由于data有一个数组,您可以执行以下操作:

var myJson = {
    "data": [{
        "id": "4831",
            "name": "Rui Vasco Martins",
            "nickname": "rvgmartins",
            "money_start": "10200",
            "balance": "8000",
            "profit": "5010",
            "nreload": "1",
            "deal_id": "4813",
            "created_at": "2015-05-27 12:54:33",
            "updated_at": "2015-07-21 22:31:47"
    }],
        "status": 200,
        "config": {
        "method": "GET",
            "transformRequest": [null],
            "transformResponse": [null],
            "url": "services/dashboard",
            "headers": {
            "Accept": "application/json, text/plain, */*"
        }
    },
        "statusText": "OK"
}
document.write('id: ' + myJson.data[0].id + '</br>name: ' + myJson.data[0].name);

回答by Lajos Arpad

First, let's understand your problem. Notes to be taken:

首先,让我们了解您的问题。需要注意的事项:

  • you have a JSON, which is inside of a variable, I will assume that this variable is foo
  • there is no such thing as data->idin Javascript. If you have an object called dataand it has a member called id, then you reference it as data.id
  • nameis inside an array. An element of an array is referenced by an index, which starts from 0, so, your array noted as datais an array having a single element, which is the 0th element
  • 你有一个 JSON,它在一个变量里面,我假设这个变量是 foo
  • data->id在 Javascript 中没有这样的东西。如果您有一个被调用的对象data并且它有一个名为 的成员id,那么您将它引用为data.id
  • name是在一个数组内。数组的元素由从 0 开始的索引引用,因此,您的数组被标记为data具有单个元素的数组,即第 0 个元素

So, you can get the idwith foo.data[0].idand you can get the nameas foo.data[0].name. However, as per se, nothing forces your array to have exactly one element. If there are more elements, you need to be able to decide somehow which element's idand nameyou are interested in. Also, your array is not forced to have elements at all.

所以,你可以得到idwith foo.data[0].id,你可以得到nameas foo.data[0].name。但是,就其本身而言,没有什么会强制您的数组只有一个元素。如果有更多的元素,你需要能够以某种方式该元素的决定idname你有兴趣。此外,您的阵列是不是被迫必须在所有元素。

Whenever you see such a JSON, arrays are enclosed inside []brackets and objects are enclosed inside {}brackets.

每当您看到这样的 JSON 时,数组都包含在[]方括号内,而对象则包含在{}方括号内。