1

I'm trying to get one attribute from one object in an observable array of objects. I can get everything except isolating the one object and getting the individual properties.

Here's the service method that's successfully getting the array:

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);
  }

Trying to get just one object isn't working:

 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);
  }

Component

this._itemsApi.getItem("test")
      .subscribe(
        item => this.testItem = item,
        error => this.errorMessage = <any>error);

Template

 <widget icon="file" [count]="testItem?.value">

JSON

["name: test, value: 1","name: test2, value: 0"]
7
  • do you have anything in console ? any error ? Commented Apr 19, 2017 at 1:02
  • Just undefined in console output Commented Apr 19, 2017 at 1:03
  • you mean Item: undefined Commented Apr 19, 2017 at 1:04
  • 2
    this is not array of objects bur array with two strings ["name: test, value: 1","name: test2, value: 0"].array of objects will be [{"name": "test", "value": 1"},{"name": "test2", "value": 0"}] Commented Apr 19, 2017 at 1:12
  • Oh man, thx Julia. I knew something was off with that. Commented Apr 19, 2017 at 1:16

2 Answers 2

2

Try this. LinQ for TypeScript. this will help you to find one from the array of objects. https://github.com/kutyel/linq.ts

Follow it's documentation it has everything you need to do.

Sign up to request clarification or add additional context in comments.

Comments

1

You have an array of strings, and not array of Objects. Reformat you API to return back array of objects -> [{"key": "value1"},{"key": "value2"}]. Then you can iterate over these objects and use item.value to get the value of a given item.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.