我希望能够访问嵌套在几个数组中的一堆密钥。我们谈论的是所有的src键,在媒体对象中 - 我如何实现它?
"attachments":[
{
"data":[
{
"url":"https://www.facebook.com/media/set/?set=pcb.1334460356630907&type=1",
"type":"album",
"title":"Photos from Rasmus Bøker Christensen's post",
"target":{
"url":"https://www.facebook.com/media/set/?set=pcb.1334460356630907&type=1",
"id":"1334460356630907"
},
"subattachments":{
"data":[
{
"url":"https://www.facebook.com/photo.php?fbid=10155421821134255&set=gm.1334460356630907&type=3",
"type":"photo",
"target":{
"url":"https://www.facebook.com/photo.php?fbid=10155421821134255&set=gm.1334460356630907&type=3",
"id":"10155421821134255"
},
"media":{
"image":{
"width":720,
"src":"https://scontent.xx.fbcdn.net/v/t1.0-0/p180x540/18194065_10155421821134255_140448089567975329_n.jpg?oh=ea225d9f9117eacf0988c90df99db4ab&oe=599424AA",
"height":540
}
}
},
{
"url":"https://www.facebook.com/photo.php?fbid=10155421821149255&set=gm.1334460356630907&type=3",
"type":"photo",
"target":{
"url":"https://www.facebook.com/photo.php?fbid=10155421821149255&set=gm.1334460356630907&type=3",
"id":"10155421821149255"
},
"media":{
"image":{
"width":540,
"src":"https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/18157176_10155421821149255_5817407499141479529_n.jpg?oh=44b9765b9db47c2ca24fa39f91c36674&oe=597A78C2",
"height":720
}
}
},
{
"url":"https://www.facebook.com/photo.php?fbid=10155421821159255&set=gm.1334460356630907&type=3",
"type":"photo",
"target":{
"url":"https://www.facebook.com/photo.php?fbid=10155421821159255&set=gm.1334460356630907&type=3",
"id":"10155421821159255"
},
"media":{
"image":{
"width":540,
"src":"https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/18194059_10155421821159255_3644927739532235006_n.jpg?oh=5b74b2b4e733ff72c9883f4692a7007d&oe=59744F95",
"height":720
}
}
}
如何使用如下示例访问src:
attachments[0].data[0].subattachments.data[0].target.url
答案 0 :(得分:0)
您可以使用此函数获取所有src
属性的值:
function deepKeyValues(obj, key) {
return Object(obj) !== obj ? []
: (key in obj ? [obj[key]] : []).concat(
...Object.keys(obj).map( k => deepKeyValues(obj[k], key) )
)
}
// Sample data
var obj = {
"attachments":[{
"data":[{
"url":"https://www.facebook.com/media/set/?set=pcb.1334460356630907&type=1",
"type":"album",
"title":"Photos from Rasmus Bøker Christensen's post",
"target":{
"url":"https://www.facebook.com/media/set/?set=pcb.1334460356630907&type=1",
"id":"1334460356630907"
},
"subattachments":{
"data":[{
"url":"https://www.facebook.com/photo.php?fbid=10155421821134255&set=gm.1334460356630907&type=3",
"type":"photo",
"target":{
"url":"https://www.facebook.com/photo.php?fbid=10155421821134255&set=gm.1334460356630907&type=3",
"id":"10155421821134255"
},
"media":{
"image":{
"width":720,
"src":"https://scontent.xx.fbcdn.net/v/t1.0-0/p180x540/18194065_10155421821134255_140448089567975329_n.jpg?oh=ea225d9f9117eacf0988c90df99db4ab&oe=599424AA",
"height":540
}
}
}, {
"url":"https://www.facebook.com/photo.php?fbid=10155421821149255&set=gm.1334460356630907&type=3",
"type":"photo",
"target":{
"url":"https://www.facebook.com/photo.php?fbid=10155421821149255&set=gm.1334460356630907&type=3",
"id":"10155421821149255"
},
"media":{
"image":{
"width":540,
"src":"https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/18157176_10155421821149255_5817407499141479529_n.jpg?oh=44b9765b9db47c2ca24fa39f91c36674&oe=597A78C2",
"height":720
}
}
}, {
"url":"https://www.facebook.com/photo.php?fbid=10155421821159255&set=gm.1334460356630907&type=3",
"type":"photo",
"target":{
"url":"https://www.facebook.com/photo.php?fbid=10155421821159255&set=gm.1334460356630907&type=3",
"id":"10155421821159255"
},
"media":{
"image":{
"width":540,
"src":"https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/18194059_10155421821159255_3644927739532235006_n.jpg?oh=5b74b2b4e733ff72c9883f4692a7007d&oe=59744F95",
"height":720
}
}
}]
}
}]
}]
};
// Demo
console.log(deepKeyValues(obj, 'src'));