gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:36:02 25 4
gpt4 key购买 nike

我试图仅获取标签下的微笑数组,然后获取属性。我尝试过搜索和简单选择。每次尝试都会产生一个 undefined variable 。如果您能解释如何选择微笑数组,那就太好了!

   {
"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"
}

最佳答案

此特定代码将聚合给定对象中的所有 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 } ]

关于javascript - 如何在多级数组中选择对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21213935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com