angular - 将 JSON 属性分配给连接的可观察数组(Typescript)
问题描述
我有一个字符串数组,我在其中循环调用休息
for (let i of this.obj_str_array) {
this.obj_array.push(this.defAPi.getDetailSatz(i.dsn, i.date));
}
将obj_array
被连接和subscribe
d
Observable.concat(...this.obj_array).subscribe(res => {
res.forEach(a => {
this.detailSatz.push(a);
});
});
这很好用。但现在我想要我的 JSON 输出中的i.dsn
from "obj_str_array"
。JSON 对象有一个名为"dsn"
. 但是dsn
可能会有所不同——例如,subscribe
d 结果的前 10 个对象可能"dsn"
与接下来的 10 个对象不同。但是每个 Rest Call 都有自己的"dsn"
.
我怎么能分配这个?
解决方案
您可以使用 spread( ...
) 和 map( .map
) 运算符Array
来做到这一点。试试这个:
Observable.concat(...this.obj_array).subscribe((res: any[]) => {
this.detailSatz = res.map((item, index) => ({ ...item, dsn: this.obj_str_array[index].dsn }))
});
推荐阅读
- python - 如何根据条件获取每一行的上一个事件
- lua - 在 Lua 中将表及其子表设置为 nil 有什么区别?
- php - 带有表单的实时更新表
- javascript - 未在 window.onload 上加载 HTML 元素
- android - pocketsphinx android 避免未列出的识别
- php - 仅当相同日期的条目不超过4个时如何插入?
- if-statement - 三元运算符 ?: 抛出关于错误参数的错误
- javascript - 在 Iterable 和返回 iterable 的函数上调用 ... of 之间的区别?
- python - 查询具有嵌套关系的数据
- machine-learning - PyTorch 中的 register_parameter 和 register_buffer 有什么区别?