如何将值附加到this.events

时间:2018-10-28 13:09:36

标签: javascript arrays

我已经尝试了一切,但无法做到这一点,不确定自己在做什么错...

我有一个数组项要传递给rest提供者...它所做的就是返回查询结果

我如何获取它存储值并将新值附加到this.events?显然,目前它仅返回测试3的信息

事件被声明为事件:可观察;

var arrInfoToFetch = ["Test 1", "Test 2", "Test 3"];

for (let i = 0; i < arrInfoToFetch .length; i++) {
  this.events = this.rest.getInfo(searchDate,arrInfoToFetch[i]);
}

1 个答案:

答案 0 :(得分:0)

首先,请注意代码中的多余空间:arrInfoToFetch .length将其修复为:

var arrInfoToFetch = ["Test 1", "Test 2", "Test 3"];

for (let i = 0; i < arrInfoToFetch.length; i++) {
  this.events = this.rest.getInfo(searchDate,arrInfoToFetch[i]);
}

第二,该事件仅返回三个,因为为您的事件分配了数组中的当前值:

 for (let i = 0; i < arrInfoToFetch.length; i++) {
      this.events = this.rest.getInfo(searchDate,arrInfoToFetch[i]);
    }

events将始终包含Test 3的值。 您应该做的是,创建一个新数组并将结果推入内部。

 var arrInfoToFetch = ["Test 1", "Test 2", "Test 3"];
 var arrInfoRecieved = new Array(arrInfoToFetch.length);
    for (let i = 0; i < arrInfoToFetch.length; i++) {
      arrInfoRecieved.push(this.rest.getInfo(searchDate,arrInfoToFetch[i]));
    }

编辑observable<any>的评论

 var arrInfoToFetch = ["Test 1", "Test 2", "Test 3"];
        for (let i = 0; i < arrInfoToFetch.length; i++) {
          this.events
            .fromEvent(this.rest.getInfo(searchDate,arrInfoToFetch[i]))
            .map(() => [arrInfoToFetch.length]) 
            .scan((list, items) => list.concat(items), [])
            .subscribe(x => console.log(x));
        }