coure2011 coure2011 - 6 months ago 28
Javascript Question

combining array objects into single object

Using Rxjs 5.0, I have an object like this

var data = [
{name: 'abc', title: 'zzz', data: '', id: ''},
{name: 'abc1', title: 'zzz1', data: '', id: ''},
{name: 'abc2', title: 'zzz2', data: '', id: ''},
{name: 'abc3', title: 'zzz3', data: '', id: ''}

I want to simple map it, to retain just name and tile. So did something like this

.map(item => return {name:, title: item.title};)
.subscribe(items => console.log('Final OBJ:' + JSON.stringify(items)));

On console I am getting items on multiple line i.e. subscribe is running 4 times. I want to run subscribe only once with one complete object containing data with only name and title fields.

Expected output on console is:

Final OBJ: [{name: 'abc', title: zzz},{name: 'abc1', title: zzz1},{name: 'abc2', title: zzz2},{name: 'abc3', title: zzz3}]


You can simply have:

  .reduce((arr, item, idx, source) => {
      title: item.title
    return arr;
  }, [])
  .subscribe(items => console.log(JSON.stringify(items)));

JS Bin example

Edit: It seems that combineAll should do the trick.

  .map(item => {
    return Rx.Observable.of({
      title: item.title
  .subscribe(items => console.log(JSON.stringify(items)));

JS Bin Example