[挑戰] 建立陣列的方法

條件

  • 陣列長度為 3

舉例

 [undefined, undefined, undefined]

new Array(3)

// 1
'123'.split('')
// 2
[...'123']
// 3
Array.from('123')
// 4
JSON.parse('[1, 2, 3]')
// 5
Object.keys({a:0, b:1, c:2})
// 6
[...Array(3).keys()]
// 7
Array.from(Array(3).keys())
// 8
[1].concat([2,3])
// 9
let arr = []
for (let i = 0; i < 3; i++) {
	arr.push(i)
}
// 10
let arr = []
let timer = setInterval(() => {
	arr.push('')
	arr.length === 3 && clearInterval(timer)
}, 0)
// 11
// chrome 69
[[1, 2], [3]].flat()
// 12
const getNewArr = (...props) => ([...props])
getNewArr(1,2,3)
// 13
[,,,]
2個讚
Array(0).fill();

沒有規定不能用 RxJS,所以挑戰全 RxJS 寫法 XD

23 種底標達成,有時間再繼續想想


    // #1
    of([1, 2, 3]).subscribe(console.log);

    // #2
    of(1, 2, 3).pipe(
      toArray()
    ).subscribe(console.log)

    // #3
    range(1, 3).pipe(
      toArray()
    ).subscribe(console.log);

    // #4
    range(1, 100).pipe(
      filter(val => val <= 3),
      toArray()
    ).subscribe(console.log);

    // #5
    from([[1, 2, 3]]).subscribe(console.log)

    // #6
    from([1, 2, 3]).pipe(
      toArray()
    ).subscribe(console.log)

    // #7
    combineLatest(
      from([0, 1]).pipe(last()),
      from([0, 1, 2]).pipe(last()),
      from([0, 1, 2, 3]).pipe(last())
    ).subscribe(console.log);

    // #8
    forkJoin(
      from([0, 1]),
      from([0, 1, 2]),
      from([0, 1, 2, 3])
    ).subscribe(console.log);

    // #9
    of(1).pipe(
      concat(of(2)),
      concat(of(3)),
      toArray()
    ).subscribe(console.log);

    // #10
    interval(0).pipe(
      take(3),
      map(val => val + 1),
      toArray()
    ).subscribe(console.log);

    // #11
    timer(0, 0).pipe(
      take(3),
      map(val => val + 1),
      toArray()
    ).subscribe(console.log);

    // #12
    interval(0).pipe(
      mapTo(1),
      take(3),
      scan((acc, one) => acc + one, 0),
      toArray()
    ).subscribe(console.log);

    // #13
    interval(0).pipe(
      take(3),
      map(val => val + 1),
      reduce((acc, one: number[]) => [...acc, one], [])
    ).subscribe(console.log);

    // #14
    interval(0).pipe(
      takeWhile(val => val < 3), 
      map(val => val + 1),
      toArray()
    )
    .subscribe(console.log);

    // #15
    const subject = new Subject(); 
    interval(0).pipe(
      tap(val => {
        if(val >= 3) {
          subject.next();
          subject.complete();
        }
      }),
      takeUntil(subject),
      map(val => val + 1),
      toArray()
    )
    .subscribe(console.log);

    // #16
    const correctResult = of([1, 2, 3]).pipe(delay(100));
    const incorrectResult = of([1, 2, 3, 4]).pipe(delay(200));
    race(correctResult, incorrectResult).subscribe(console.log);

    // #17
    const zip1 = of(1);
    const zip2 = of(2);
    const zip3 = of(3);
    zip(zip1, zip2, zip3).subscribe(console.log);

    // #18
    const merge1 = of(1).pipe(delay(1));
    const merge2 = of(2).pipe(delay(2));
    const merge3 = of(3).pipe(delay(3));
    merge(merge1, merge2, merge3).pipe(toArray()).subscribe(console.log);

    // #19
    interval(0).pipe(
      take(3),
      map(val => of(val + 1)),
      concatAll(),
      toArray()
    ).subscribe(console.log);


    // #20
    interval(0).pipe(
      take(3),
      map(val => of(val + 1)),
      mergeAll(),
      toArray()
    ).subscribe(console.log);

    // #21
    interval(0).pipe(
      take(3),
      map(val => of(val + 1)),
      combineAll()
    ).subscribe(console.log);

    // #22
    const appendInnerValueToResult = innerValue => result => of(innerValue).pipe(map(val => [...result, val]));
    of([]).pipe(
      concatMap(appendInnerValueToResult(1)),
      concatMap(appendInnerValueToResult(2)),
      concatMap(appendInnerValueToResult(3)),
    ).subscribe(console.log);

    // #23
    const arr = [];
    const seconds = interval(10);
    seconds.pipe(
      timeout(1),
      catchError(_ => {
        arr.push(arr.length + 1);
        throwError()
        return of();
      }),
      retry(2),
      catchError(_ => of(arr))
    ).subscribe(console.log);
3個讚

再新增 5 種

    // #24
    const defaultSubject = new Subject();
    defaultSubject.pipe(toArray()).subscribe(console.log);
    defaultSubject.next(1);
    defaultSubject.next(2);
    defaultSubject.next(3);
    defaultSubject.complete();

    // #25
    const behaviorSubject = new BehaviorSubject(1);
    behaviorSubject.pipe(toArray()).subscribe(console.log);
    behaviorSubject.next(2);
    behaviorSubject.next(3);
    behaviorSubject.complete();

    // #26
    const replaySubject = new ReplaySubject();
    replaySubject.next(1);
    replaySubject.next(2);
    replaySubject.next(3);
    replaySubject.complete();

    replaySubject.pipe(toArray()).subscribe(console.log);

    // #27
    const asyncSubject = new AsyncSubject();
    asyncSubject.subscribe(console.log);
    of(1, 2, 3).pipe(toArray()).subscribe(asyncSubject);
    asyncSubject.complete();

    // #28
    Observable.create((observer: Observer<number>) => {
      observer.next(1);
      observer.next(2);
      observer.next(3);
      observer.complete();
    }).pipe(toArray()).subscribe(console.log);
4個讚