arrays - 将新的 JSON 对象附加到 Polymer 中的数组

标签 arrays polymer-2.x

我正在使用 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/

相关文章:

css - 在 Polymer 中设置样式时考虑 parent 的宽度/高度

ruby - 您如何保证数组具有最低级别的递归?

python - 将两个 numpy 数组中的值与 'if' 进行比较

c++ - 在 C++ 中读入数组

linux - Polymer serve 拒绝外部连接

javascript - 不同ShadowDom中的Polymer 2.0 getElementById

javascript - Polymer 2.0 中何时使用构造函数和 super?

c - 尝试获取二维数组的列长度时,数组下标的无效类型 'float[int]'

javascript - 如何遍历对象数组并制作键值对?

javascript - 在 vaadin 日期选择器中禁用整个月