-2

I have a service ice that pulls an array from the back-end, I need to get a single item from the array based on the ID, how do I do this in Angular?

enter image description here

I'm looking for how to do it specifically with angulars pipeable operators. Filter tends to just stop everything from working without throwing errors if not done correctly. I can get the the row info but I cant target the properties i get "Cannot read property 'id' of undefined"

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • 1
    better is if you post your code as well what have you tried so far – Pardeep Jain Apr 24 '18 at 12:56
  • 3
    Possible duplicate of [Javascript: How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – bugs Apr 24 '18 at 12:57
  • I'm looking for how to do it specifically with angulars pipeable operators. Filter tends to just stop everything from working without throwing errors if not done correctly. I can get the the row info but I cant target the properties i get "Cannot read property 'id' of undefined" – Pam Halligan-Sims Apr 24 '18 at 13:03

4 Answers4

1

angulars pipeable operators

I am not entirely sure what you mean by angulars pipeable operators. You have the tag rxjs-pipeable-operators in your question, so I expect you mean rxjs operators in general.

So I make the assumption on what you actually want to do. Please leave a comment if I missunderstood.

You have a service which returns an Observable from an array. Something like this:

function getMyData(): Observable<any[]> {
    return Observable.create(observer => {
        observer.next([
            { name: 'value 1', id: 1 },
            { name: 'value 2', id: 2 },
            { name: 'value 3', id: 3 }
        ]);

    });
}

If you want to modify the value in this observable stream, you cannot use filter or find on the stream directly. The value you receive is the full array, so the find operation is executed on the array object and not the individual items. What you want to do is to map the resulting value and filter it in the mapping function.

getMyData()
    .map((theArray: any[]) => theArray.find(item => item.id === 2))
tom van green
  • 1,674
  • 10
  • 14
0

I need to get a single item from the array based on the ID

try with Array.find()

let data = yourArray.find(x => x.id == "en-gb");

or try with array().filter().shift()

Please go through this below discussion

Difference between find() and filter().shift() on javascript

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • This is exactly what I needed thanks. I had needed to rewrite the code, filter had been working perfectly originally but with the rewrite it just stopped producing any results at all. – Pam Halligan-Sims Apr 25 '18 at 07:35
0
 this.booksByStoreID = this.books.filter(
      book => book.store_id === this.store.id);

How do I filter an array with TypeScript in Angular 2?

Daddelbob
  • 406
  • 4
  • 13
0

for single item from an array.

let selectedId = Array.find(a => a.id == 'fr-fr');
M Rameez
  • 189
  • 1
  • 3
  • 10