所以我有以下JSON,我试图遍历它,找到与sdate
匹配的2019-07-13 14:40:50 GMT
,但返回link
值。
最好的方法是什么?
{
"status": "ok",
"data": {
"Gallery": {
"gallery_id": "***ID***",
"GalleryImage": [{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "URL"
},
"Iptc": {
"sdate": "2019-07-11 14:40:50 GMT"
}
}
},
{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "***URL***"
},
"Iptc": {
"sdate": "2019-07-12 14:40:50 GMT"
}
}
},
{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "***URL***"
},
"Iptc": {
"sdate": "2019-07-13 14:40:50 GMT"
}
}
}
]
}
}
}
我的理解是我需要使用for
循环,例如:
for (var key in **ID**) {
if (**ID*.hasOwnProperty(key)) {
}
}
在Panos K的帮助下,我有以下正在运行的方法,但这可能不是最佳方法吗?
let initArray = {
"status": "ok",
"data": {
"Gallery": {
"gallery_id": "***ID***",
"GalleryImage": [{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "LINK1"
},
"Iptc": {
"sdate": "2019-07-11 14:40:50 GMT"
}
}
},
{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "LINK2"
},
"Iptc": {
"sdate": "2019-07-13 14:40:50 GMT"
}
}
},
{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "LINK3"
},
"Iptc": {
"sdate": "2019-07-13 14:40:50 GMT"
}
}
}
]
}
}
}
function getImgLinksByDate(initArray, date){
return initArray.data
.Gallery
.GalleryImage
.filter(i=>i.Image.Iptc.sdate==date)
.map(i=>i.Image.ImageLink.link);
}
links = getImgLinksByDate(initArray, '2019-07-13 14:40:50 GMT');
links.forEach(function (item) {
var div = document.getElementById('divID');
div.innerHTML += '<div>' + item + '</div>';
});
答案 0 :(得分:1)
您可以使用此方法,我使用了一些硬代码值,假设该值保持不变,则可以添加代码是否存在检查
将其视为sudo示例
$link->error
答案 1 :(得分:1)
使用Array.find()
let data = {
"status":"ok",
"data":{
"Gallery":{
"gallery_id":"***ID***",
"GalleryImage":[
{
"image_id":"***ID***",
"gallery_id":"***ID***",
"Image":{
"image_id":"***ID***",
"gallery_id":"***ID***",
"ImageLink":{
"link":"URL"
},
"Iptc":{
"sdate":"2019-07-11 14:40:50 GMT"
}
}
},
{
"image_id":"***ID***",
"gallery_id":"***ID***",
"Image":{
"image_id":"***ID***",
"gallery_id":"***ID***",
"ImageLink":{
"link":"***URL***"
},
"Iptc":{
"sdate":"2019-07-12 14:40:50 GMT"
}
}
},
{
"image_id":"***ID***",
"gallery_id":"***ID***",
"Image":{
"image_id":"***ID***",
"gallery_id":"***ID***",
"ImageLink":{
"link":"***URL***"
},
"Iptc":{
"sdate":"2019-07-13 14:40:50 GMT"
}
}
}
]
}
}
};
function getLinkByDate(data, date) {
const found = data.data.Gallery.GalleryImage.find(gi => gi.Image.Iptc.sdate === date);
return found ? found.Image.ImageLink.link : undefined;
}
console.log(getLinkByDate(data, "2019-07-13 14:40:50 GMT"));
答案 2 :(得分:0)
您可以像这样使用filter和map制作更通用的方法(如果两次发现日期,也会返回数组中的url)
function getImgLinksByDate(initArray, date){
return initArray.data
.Gallery
.GalleryImage
.filter(i=>i.Image.Iptc.sdate==date)
.map(i=>i.Image.ImageLink.link);
}