我知道我必须遗漏一些非常基本的东西,但我能用一双新鲜的眼睛来做。
我有一个对象数组(参见代码)。
我正在尝试读取每个对象的属性variants
的值。
我尝试过很多种组合。例如,如果我object.variants
或object[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"
]
}
]
答案 0 :(得分:3)
假设您有一个包含此内容的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);
}
答案 1 :(得分:1)
您可以使用此jsfiddle
之类的forEach()
功能
objects.forEach(function(element, index, array) {
console.log(element.variants);
});
答案 2 :(得分:1)
您有一个对象数组,其中每个对象包含更多数组的键。使用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>");
});
&#13;
答案 3 :(得分:0)
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"]);
}
答案 4 :(得分:0)
确保将其分配给变量
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)
&#13;
答案 5 :(得分:0)
还有一个: - )
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 ;-)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>
&#13;