如何在多级数组中选择对象

时间:2014-01-19 06:29:44

标签: javascript json api

我试图在标签和属性下获得微笑数组。我试过两个搜索,只是选择。每次尝试都会产生一个未定义的变量。如果你能解释如何选择非常棒的微笑阵列!

   {
        "status": "success",
        "photos": [{
            "url": "http://tinyurl.com/673cksr",
            "pid": "F@019cbdb135cff0880096136c4a0b9bad_3547b9aba738e",
            "width": 375,
            "height": 406,
            "tags": [{
                "uids": [],
                "label": null,
                "confirmed": false,
                "manual": false,
                "width": 30.67,
                "height": 28.33,
                "yaw": -16,
                "roll": -1,
                "pitch": 0,
                "attributes": {
                    "face": {
                        "value": "true",
                        "confidence": 84
                    },
                    "smiling": {
                        "value": "false",
                        "confidence": 46
                    }
                },
                "points": null,
                "similarities": null,
                "tid": "TEMP_F@019cbdb135cff0880096136c00d500a7_3547b9aba738e_56.80_41.13_0_1",
                "recognizable": true,
                "center": {
                    "x": 56.8,
                    "y": 41.13
                },
                "eye_left": {
                    "x": 66.67,
                    "y": 35.71,
                    "confidence": 51,
                    "id": 449
                },
                "eye_right": {
                    "x": 50.67,
                    "y": 35.47,
                    "confidence": 57,
                    "id": 450
                },
                "mouth_center": {
                    "x": 60.8,
                    "y": 51.23,
                    "confidence": 53,
                    "id": 615
                },
                "nose": {
                    "x": 62.4,
                    "y": 42.61,
                    "confidence": 54,
                    "id": 403
                }
            }]
        }],
        "usage": {
            "used": 21,
            "remaining": 79,
            "limit": 100,
            "reset_time": 1390111833,
            "reset_time_text": "Sun, 19 January 2014 06:10:33 +0000"
        },
        "operation_id": "edc2f994cd8c4f45b3bc5632fdb27824"
    }

4 个答案:

答案 0 :(得分:2)

此特定代码将聚合来自给定对象的所有smiling属性,并将其作为数组返回。

  • map函数会从每个标记中获取smiling属性

  • concat函数将展平所有属性,并为每张照片返回一个数组。

  • reduce函数将收集所有照片的所有属性,并将结果累积到result中,并在最后返回。


var result = data.photos.reduce(function(result, currentPhoto) {
    return result.concat(currentPhoto.tags.map(function(currentTag) {
        return currentTag.attributes.smiling;
    }));
}, []);

console.log(result);

<强>输出

[ { value: 'false', confidence: 46 } ]

答案 1 :(得分:1)

JSON.parse(json).photos[0].tags[0].attributes.smiling

答案 2 :(得分:1)

obj.photos[0].tags[0].attributes.smiling

最好的方法是遍历标记,而不是在那里硬编码0

obj.photos.forEach(function(photo){
  photo.tags.forEach(function(tag){ 
    tag.attributes.smiling; // here it is
  });
});

答案 3 :(得分:1)

这有点棘手,因为你的JSON对象是对象和数组的混合,但是这里是你如何得到“微笑”的对象(它是一个对象,因为JavaScript中没有关联数组):

var smiling_object = obj["photos"][0]["tags"][0]["attributes"]["smiling"];

然后,如果你想用它做点什么:

var some_var = smiling_object["value"];
var some_other_var = smiling_object["confidence"];

alert("The combined string is " + some_var + " and " + some_other_var);