Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

groupby angular

content_copyopen_in_new
import { of } from 'rxjs';
import { mergeMap, groupBy, reduce } from 'rxjs/operators';
 
of(
  {id: 1, name: 'JavaScript'},
  {id: 2, name: 'Parcel'},
  {id: 2, name: 'webpack'},
  {id: 1, name: 'TypeScript'},
  {id: 3, name: 'TSLint'}
).pipe(
  groupBy(p => p.id),
  mergeMap((group$) => group$.pipe(reduce((acc, cur) => [...acc, cur], [])))
)
.subscribe(p => console.log(p));
 
// displays:
// [ { id: 1, name: 'JavaScript'},
//   { id: 1, name: 'TypeScript'} ]
//
// [ { id: 2, name: 'Parcel'},
//   { id: 2, name: 'webpack'} ]
//
// [ { id: 3, name: 'TSLint'} ]
Comment

groupby angular

content_copyopen_in_new
import { of } from 'rxjs';
import { groupBy, map, mergeMap, reduce } from 'rxjs/operators';
 
of(
  { id: 1, name: 'JavaScript' },
  { id: 2, name: 'Parcel' },
  { id: 2, name: 'webpack' },
  { id: 1, name: 'TypeScript' },
  { id: 3, name: 'TSLint' }
)
  .pipe(
    groupBy(p => p.id, p => p.name),
    mergeMap(group$ =>
      group$.pipe(reduce((acc, cur) => [...acc, cur], [`${group$.key}`]))
    ),
    map(arr => ({ id: parseInt(arr[0], 10), values: arr.slice(1) }))
 )
 .subscribe(p => console.log(p));
 
// displays:
// { id: 1, values: [ 'JavaScript', 'TypeScript' ] }
// { id: 2, values: [ 'Parcel', 'webpack' ] }
// { id: 3, values: [ 'TSLint' ] }
Comment

PREVIOUS NEXT
Code Example
Javascript :: how play audio js 
Javascript :: react lazy load suspense 
Javascript :: mongoose db connect 
Javascript :: launch uikit modal from php 
Javascript :: how to generate random array in javascript 
Javascript :: firefox freeze page 
Javascript :: Dart regex all matches 
Javascript :: javascript image to variable 
Javascript :: convert a string to array in javascript 
Javascript :: javascript cartesian product 
Javascript :: for value in array javascript 
Javascript :: axios get method 
Javascript :: display image on button click javascript 
Javascript :: how to convert string into binary in javascript 
Javascript :: jquery find element before 
Javascript :: split string into two parts javascript 
Javascript :: farewell discord.js 
Javascript :: node convert string to hash 
Javascript :: foreach loop js arrow functons 
Javascript :: jquery validation on click 
Javascript :: how to see if user on phone 
Javascript :: firebase signout 
Javascript :: mongodb find array which does not contain object 
Javascript :: recursive reverse string 
Javascript :: pdf to html js 
Javascript :: JS how to determine if page was cached 
Javascript :: kendo datasource get 
Javascript :: check if variable is set javascript 
Javascript :: javascript get type of var 
Javascript :: change class of icon using jquery 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =