簡體   English   中英

如何訪問對象數組的屬性

[英]How to access property of array of objects

我知道我在這里一定會錯過一些非常基本的東西,但是我可以用一雙新鮮的眼睛做。

我有一個對象數組(請參見代碼)。

我正在嘗試讀取每個對象的屬性variants的值。

我嘗試了很多組合。 例如,如果我做object.variantsobject[0].variants ,結果是undefined

有人可以告訴我為什么嗎?

更新:我需要遍歷數組中的對象,並為每個對象檢查variants屬性。 如果它不為null則獲取其值並記錄下來。

   [
    {
        "type": "text",
        "complete": true,
        "startPoint": {
            "x": 233.84,
            "y": 82.13
        },
        "endPoint": {
            "x": 461.27,
            "y": 79.74
        },
        "text": "xvxvs xsvx vx",
        "variants": [
            "xvxvs",
            "xsvx",
            "vx"
        ]
    },
    {
        "type": "text",
        "complete": true,
        "startPoint": {
            "x": 515.39,
            "y": 120.84
        },
        "endPoint": {
            "x": 803.67,
            "y": 111.31
        },
        "text": "casdc adcasdcasc",
        "variants": [
            "casdc",
            "adcasdcasc"
        ]
    }
]

好吧,假設您有一個具有以下內容的javascript變量:

var obj = 
    [
        {
            "type": "text",
            "complete": true,
            "startPoint": {
                "x": 233.84,
                "y": 82.13
            },
            "endPoint": {
                "x": 461.27,
                "y": 79.74
            },
            "text": "xvxvs xsvx vx",
            "variants": [
                "xvxvs",
                "xsvx",
                "vx"
            ]
        },
        {
            "type": "text",
            "complete": true,
            "startPoint": {
                "x": 515.39,
                "y": 120.84
            },
            "endPoint": {
                "x": 803.67,
                "y": 111.31
            },
            "text": "casdc adcasdcasc",
            "variants": [
                "casdc",
                "adcasdcasc"
            ]
        }
    ];

那么您可以像這樣訪問變體內容:

alert(obj[0].variants);

這是它的工作演示: http : //jsfiddle.net/66o8h5de/

因此,基本上obj是一個數組,而obj[0]表示此數組的第一個元素,而obj[0].variants是此數組的第一個元素的variants屬性。 當然,根據您的需要,您可能希望遍歷此obj數組的元素,在這種情況下,可以使用for循環:

for(var i = 0; i < obj.length; i++) {
    var element = obj[i];
    alert(element.variants);
}

您可以使用像jsfiddle這樣的forEach()函數

objects.forEach(function(element, index, array) {
   console.log(element.variants); 
});

您有一個對象數組,其中每個對象都包含更多數組的鍵。 使用forEach遍歷對象,然后再次遍歷變量數組的方法:

 var array = [{"type":"text","complete":true,"startPoint":{"x":233.84,"y":82.13},"endPoint":{"x":461.27,"y":79.74},"text":"xvxvs xsvx vx","variants":["xvxvs","xsvx","vx"]},{"type":"text","complete":true,"startPoint":{"x":515.39,"y":120.84},"endPoint":{"x":803.67,"y":111.31},"text":"casdc adcasdcasc","variants":["casdc","adcasdcasc"]}] array.forEach(function(element) { variantArray = element.variants; variantArray.forEach(function(variant) { document.write(variant + "<br>"); }); document.write("<br>"); }); 

var arr =  [
    {
        "type": "text",
        "complete": true,
        "startPoint": {
            "x": 233.84,
            "y": 82.13
        },
        "endPoint": {
            "x": 461.27,
            "y": 79.74
        },
        "text": "xvxvs xsvx vx",
        "variants": [
            "xvxvs",
            "xsvx",
            "vx"
        ]
    },
    {
        "type": "text",
        "complete": true,
        "startPoint": {
            "x": 515.39,
            "y": 120.84
        },
        "endPoint": {
            "x": 803.67,
            "y": 111.31
        },
        "text": "casdc adcasdcasc",
        "variants": [
            "casdc",
            "adcasdcasc"
        ]
    }
];


var len = arr.length;

for(var i=0;i<len;i++){
  var obj = arr[i];
  console.log(obj["variants"]);
}

確保將其分配給變量

 var arr = [ { "type": "text", "complete": true, "startPoint": { "x": 233.84, "y": 82.13 }, "endPoint": { "x": 461.27, "y": 79.74 }, "text": "xvxvs xsvx vx", "variants": [ "xvxvs", "xsvx", "vx" ] }, { "type": "text", "complete": true, "startPoint": { "x": 515.39, "y": 120.84 }, "endPoint": { "x": 803.67, "y": 111.31 }, "text": "casdc adcasdcasc", "variants": [ "casdc", "adcasdcasc" ] } ] console.log(arr[0].variants) 

還有一個:-)

 var data = [ { "type": "text", "complete": true, "startPoint": { "x": 233.84, "y": 82.13 }, "endPoint": { "x": 461.27, "y": 79.74 }, "text": "xvxvs xsvx vx", "variants": [ "xvxvs", "xsvx", "vx" ] }, { "type": "text", "complete": true, "startPoint": { "x": 515.39, "y": 120.84 }, "endPoint": { "x": 803.67, "y": 111.31 }, "text": "casdc adcasdcasc", "variants": [ "casdc", "adcasdcasc" ] } ] ; for ( var i in data ) { $('pre').append( data[i].variants + "\\n"); } And one more ;-) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <pre></pre> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM