Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2订阅可观察数组的属性_Angular_Typescript_Rxjs - Fatal编程技术网

Angular 角度2订阅可观察数组的属性

Angular 角度2订阅可观察数组的属性,angular,typescript,rxjs,Angular,Typescript,Rxjs,我试图从一个可观察的对象数组中的一个对象获取一个属性。除了隔离一个对象和获取单个属性之外,我可以获取所有内容 以下是成功获取阵列的服务方法: getItems(): Observable<IItem[]> { return this.get(this.baseUrl + '/items') .map((res: Response) => <IItem[]>res.json()) .do(data => console.log("

我试图从一个可观察的对象数组中的一个对象获取一个属性。除了隔离一个对象和获取单个属性之外,我可以获取所有内容

以下是成功获取阵列的服务方法:

getItems(): Observable<IItem[]> {
    return this.get(this.baseUrl + '/items')
      .map((res: Response) => <IItem[]>res.json())
      .do(data => console.log("Items: " + JSON.stringify(data)))
      .catch(this.handleError);
  }

您有一个字符串数组,而不是对象数组。
重新格式化API以返回对象数组->[{“key”:“value1”},{“key”:“value2”}]。然后您可以迭代这些对象并使用item.value获取给定项的值。

试试这个。LinQ用于TypeScript。这将帮助您从对象数组中找到一个。


遵循它的文档,它有您需要做的一切。

控制台中有什么东西吗?有错误吗?只是控制台输出中未定义您的意思是
Item:undefined
这不是对象数组,而是带有两个字符串[“name:test,value:1”,“name:test2,value:0”]的数组。对象数组将是[{“name”:“test”,“value”:1”},{“name”:“test2”,“value”:0”}噢,伙计,谢谢Julia。我知道这件事出了问题。
 getItem(name: String): Observable<IItem> {
    return this.getItems()
      .map((items: IItem[]) => items.find(item => item.name === name))
      .do(item => console.log("Item: " + item)) 
      .catch(this.handleError);
  }
this._itemsApi.getItem("test")
      .subscribe(
        item => this.testItem = item,
        error => this.errorMessage = <any>error);
 <widget icon="file" [count]="testItem?.value">
["name: test, value: 1","name: test2, value: 0"]