我正在尝试按名为“事件日期”的元字段对 Shopify 博客文章进行排序。我调用了我的articles.JSON,但它没有附带metafields.JSON,因此我必须获取该数组并将其放入foreach 函数中以将元字段附加到每篇文章。
这是每篇文章的 metafields.json 的设置方式:
{
"metafields":[
{
"id":5994805788772,
"namespace":"global",
"key":"Event-Date",
"value":"1549256400",
"value_type":"string",
"description":null,
"owner_id":15977611364,
"created_at":"2019-02-06T18:31:44-05:00",
"updated_at":"2019-02-06T18:31:44-05:00",
"owner_resource":"article"
},
{
"id":5994805821540,
"namespace":"global",
"key":"Event-Time",
"value":"6:00pm - 8:00pm",
"value_type":"string",
"description":null,
"owner_id":15977611364,
"created_at":"2019-02-06T18:31:45-05:00",
"updated_at":"2019-02-06T18:31:45-05:00",
"owner_resource":"article"
},
{
"id":6010564542564,
"namespace":"global",
"key":"Location",
"value":"18th Street Location",
"value_type":"string",
"description":null,
"owner_id":15977611364,
"created_at":"2019-02-07T13:16:05-05:00",
"updated_at":"2019-02-07T14:05:08-05:00",
"owner_resource":"article"
}
]
}
我如何在下面附加 metafields.JSON:
var request = new XMLHttpRequest();
request.open('GET', '/admin/blogs/43130421348/articles.json');
request.responseType = 'json';
request.send();
request.onload = function() {
var articleList = request.response;
var articleArray = articleList.articles;
var date = new Date();
var ticks = Math.floor(date.getTime() / 1000);
var count = 0;
articleArray.forEach(function(entry,index, object){
var metaRequest = new XMLHttpRequest();
metaRequest.open('GET', '/admin/blogs/43130421348/articles/'+ entry.id + '/metafields.json');
metaRequest.responseType = 'json';
metaRequest.send();
console.log(index);
metaRequest.onload = function() {
var articleMetaObj = metaRequest.response;
var articleMetaArr = articleMetaObj.metafields;
entry.metafields = articleMetaArr;
var eventDate = entry.metafields[0].value;
}
});
};
我现在正试图摆脱与当前日期相比已经过去的日期(“Key”:“Event-Date”)的任何文章。我查看了以下有关在 foreach 循环中删除对象的Stack Overflow 帖子,但没有一个解决方案证明实际上可以摆脱所有文章。它偶尔会摆脱所有这些,但有时会留在其中一个物体中。
我也尝试了一个数组过滤器,但是当我使用它时,我得到的只是一个空数组。我现在已经坚持了一段时间,因此非常感谢任何有关解决它的帮助。