3

Im requesting a web service and receiving an observable and loop them to build another array of objects like below.

export interface Seller {
  email?:String;
  isOnline?:boolean;
  name?:String;
}

Angular Component :

sellers: Observable<Seller[]>;

constructor(firestore: AngularFirestore) {
    this.sellers = firestore.collection('sellers').valueChanges();
 }

getSllers(){
    return this.sellers;
 }

Using these sellers Observable im creating an array of objects.

getSellerDetails() {
    return this.getSllers().pipe(
        map(sellers => {
        sellers.filter(seller => seller.isOnline == true)
         return sellers.map(seller => {
            return {
                name: seller.name,
                isOnline: seller.isOnline,
                email: seller.email
            }
        });
    })); 
 }

And here i want to filter out the sellers only who are having isOnline true. The snippet im having seems not working. What would be the best approach?

4
  • 1
    You have a mistake in your map callback. You are not doing anything with the array returned by the Array.filter method called on sellers. Commented May 31, 2021 at 18:32
  • Can you put an answer please Commented May 31, 2021 at 18:37
  • is selleers=sellers.filter(seller => seller.isOnline == true) -a filter of array return a new array but not change the original array- Commented May 31, 2021 at 18:46
  • @Eliseo can you put an answer? Commented May 31, 2021 at 18:55

3 Answers 3

5
getSellerDetails() {
    return this.getSllers().pipe(
        // Filter only sellers that isOnline
        map(sellers => sellers.filter(seller => seller.isOnline == true)),
        // Map all filtered sellers to the wanted interface
        map(sellers => {
            return sellers.map(seller => {
              return {
                name: seller.name,
                isOnline: seller.isOnline,
                email: seller.email
              }
           });
        })
      )
 }

Also can be simplify with:

getSellerDetails() {
    return this.getSllers().pipe(
        // Filter only sellers that isOnline
        map((sellers) => sellers.filter((s) => s.isOnline)),
        // Map all filtered sellers to the wanted interface
        map((sellers) => sellers.map(s => ({
           name: s.name,
           isOnline: s.isOnline,
           email: s.email
        })),
 }

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

Comments

1

Mikkel is right. I think you mean to do:

return sellers
 // filter the array
 .filter(selller => seller.isOnline === true)
 // map the filtered array
 .map(seller => {
  return {
    name: seller.name,
    isOnline: seller.isOnline,
    email: seller.email,
  };
});

Comments

0
getSellerDetails() {
    return this.getSllers().pipe(
        map(sellers => 
         sellers
         .filter(seller => seller.isOnline)
         .map(seller => {
            return {
                name: seller.name,
                isOnline: seller.isOnline,
                email: seller.email
            }
        })
    )); 
 }

It looks like you didn't use seller filter result for mapping. Try this!

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.