我正在使用 REST API 来获取 Polymer 2.0.2 项目中的对象数组。响应是这样的:
[
{"name":"John","city":"Mumbai"},
{"name":"Ron","city":"New York"},
{"name":"Harry","city":"Lisbon"}
]
收到响应后,我将名为 content
的属性设置如下:
_contentAjaxResponseHandler(event) {
this.set('content', event.detail.response);
}
只要调用 REST API 一次,此操作就有效。 现在,我想在用户滚动到页面底部时获取下一批并将其添加到现有数据中。
所以,我的问题是,将新结果附加到现有 content
数组的最佳方法是什么?或者换句话说,在聚合物中合并 2 个数组的最佳方法是什么?
到目前为止,我能想到的唯一方法是循环新结果并调用 push
方法。如下所示:
_contentAjaxResponseHandler(event) {
let newResponse = event.detail.response;
newResponse.forEach(function(newObj){
this.push('content',newObj);
});
}
最佳答案
以下代码对我有用:
_contentAjaxResponseHandler(event) {
let newResponse = event.detail.response;
this.set('content',this.content.concat(newResponse));
}
关于arrays - 将新的 JSON 对象附加到 Polymer 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45712632/