首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在多级数组中选择对象

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

Stack Overflow用户
提问于 2014-01-19 06:29:44
回答 4查看 48关注 0票数 0

我试图得到标签下的微笑数组,然后是属性。我已经尝试了搜索和简单选择。每次尝试都会产生一个未定义的变量。如果你能解释如何选择微笑数组,那将是非常好的!

代码语言:javascript
复制
   {
        "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"
    }
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-01-19 06:33:59

代码语言:javascript
复制
JSON.parse(json).photos[0].tags[0].attributes.smiling
票数 1
EN

Stack Overflow用户

发布于 2014-01-19 06:35:04

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

  • map函数将从每个标记中获取smiling属性。
  • concat函数将扁平所有属性,并返回每个照片的一个数组。
  • reduce函数将收集所有属性、所有照片并在result中积累结果,并在最后返回。

代码语言:javascript
复制
var result = data.photos.reduce(function(result, currentPhoto) {
    return result.concat(currentPhoto.tags.map(function(currentTag) {
        return currentTag.attributes.smiling;
    }));
}, []);

console.log(result);

输出

代码语言:javascript
复制
[ { value: 'false', confidence: 46 } ]
票数 2
EN

Stack Overflow用户

发布于 2014-01-19 06:34:27

代码语言:javascript
复制
obj.photos[0].tags[0].attributes.smiling

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

代码语言:javascript
复制
obj.photos.forEach(function(photo){
  photo.tags.forEach(function(tag){ 
    tag.attributes.smiling; // here it is
  });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21213935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档