我有Java服务,该服务从oracle数据库中检索表,并且我想在Angular应用程序中显示结果,并且在Angular中还有一个对象数组:
resultSet:Info[]=[];
服务:
pastHourInfo() {
const url='/SearchApp-1.0/users/transaction';
return this.http.get(url).pipe(map((data:any)=>data));
}
这是我的服务订阅:
checkPasHourInfo() {
this.searchService.pastHourInfo().subscribe(data => {
console.log("we got: ",data);
this.resultSet.push(data);
console.log(this.resultSet.length);
},
error => {
console.log("Error",error);
},);
下一个问题。结果是77行。
console.log("we got: ",data)
给出正确的结果。 you cans see it here
但是console.log(this.resultSet.length);
在必须为77时会显示“ 1”。
出什么问题了?
答案 0 :(得分:1)
您正在将数组推入数组。所以你的数组看起来像这样
resultSet[].length = 1;
resultSet[0].length = 77;
代替:
this.resultSet.push(data);
尝试一下:
this.resultSet.concat(data);
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
答案 1 :(得分:1)
从屏幕快照中可以看出,您正在将array
推入结果array
中,可以像这样将数据散布到数组中:
this.resultSet.push(...data);
答案 2 :(得分:0)
您正在将整个数组推入一个数组中,您可以试试看
data.forEach(result => {
this.resultSet.push(result)
});
仅在上面将其用于效率低的情况。
this.resultSet.push(...data) // this.resultSet.concat(data)
spread运算符在这里很好用(...利用数组中的所有元素)
您目前正在做什么
this.resultSet = [ [77 objects here] ] //pseudo
因此获取长度将返回一个。
这应该给出您想要的答案。
答案 3 :(得分:0)
您可以将数组添加到另一个数组中,如下所示:
this.resultSet.concat(data)