2

I have a collection that looks like this

[
  {
    count: 123,
    description: 'some description',
    articles: [
     {...}
    ]
  },
  {
    count: 234,
    description: 'some description',
    articles: [
      {...}
    ]
  }
]

Each object in the collection has a collection of articles. What I need is to apply the description to each article object in the respective collection in each element of the primary collection. I also want to end up with a flat array containing only articles. Clearly I'm using mergeMap incorrectly, but I'm not sure how to do it.

I have tried this

json$.pipe(
    // Filter out empty arrays
    filter(section => section.count > 0),
    // Apply the descriptions 
    map(section => section.articles.map(a => (Object.assign(a, section.sectionName)))),
    mergeMap(x => x.articles)
).subscribe(x => console.log(x));

But the articles do not have the description property in them, and it's not a flat array of articles. I've tried a few things but I'm unsure how to proceed

2
  • What do you mean by "apply the description to each article object"? Commented May 18, 2018 at 7:13
  • I meant that I wanted to apply the description property to each article object in the articles array. Like articles.map(a => ({...a, description}). Yoshi has supplied a solution that takes of it Commented May 19, 2018 at 4:30

1 Answer 1

1

You only need to concatMap the outer observable, after adjusting each article.

const { Observable } = Rx;
const { map, concatMap, filter } = Rx.operators;

const json$ = Observable.from([
  {
    count: 123,
    description: 'some description 123',
    articles: [
      {id: 1},
      {id: 2},
    ]
  },
  {
    count: 234,
    description: 'some description 234',
    articles: [
      {id: 3},
      {id: 4},
    ]
  }
]);

const withDescription$ = json$.pipe(
  filter(({count}) => count > 0),
  concatMap(
    ({articles, description}) => Observable.from(articles).map(a => ({...a, description}))
  )
);

withDescription$.subscribe(console.log);
<script src="https://unpkg.com/@reactivex/rxjs@^5/dist/global/Rx.min.js"></script>


If you don't need any special behavior on the inner observable, you could simplify to:

const withDescription$ = json$.pipe(
  filter(({count}) => count > 0),
  concatMap(
    ({articles, description}) => articles.map(a => ({...a, description}))
  ),
);
Sign up to request clarification or add additional context in comments.

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.