2016-07-27 4 views
0

배열을 반복하고, 다른 배열의 해당 객체를 찾아 객체의 결과를 병합해야합니다. 나는 내가하고 싶은 것을중첩 된 observables없이 여러 배열을 반복하는 방법

var usersObservable = RX.Observable.fromArray(users); 
var typesObservable = Rx.Observable.fromArray(types); 
var levelsOBservable = Rx.Observable.fromArray(levels); 

var uiUsers= [];// not really needed because I will use the same users array again. 

usersObservable.map(function(user) { 
     typesObservable.filter(function(type) { 
     return type.id == user.type; 
    }).subscribeOnNext(function(userType) { 
     user.typeDescription = userType.description; 
    }); 
    return user; 
}).map(function(user) { 
     levelsOBservable.filter(function(level) { 
     return level.id == user.levelId; 
    }).subscribeOnNext(function(level) { 
     user.levelDescription = level.description; 
    }); 
    return user; 
}) 
.subscribeOnNext(function(user) { 
    uiUsers.push(user); 
}) 

처럼 달성 할 수 알고

var users = [ 
    { name: "A", type: 2, level: 1, levelDescription: "Level 1", typeDescription: "Type 2" }, 
    { name: "B", type: 1, level: 2, levelDescription: "Level 2", typeDescription: "Type 1" } 
] 

:

내가 그 결과 다음과 같은 갖고 싶어 내가 세 가지 배열
var users = [ 
    { name: "A", type: 2, level: 1 }, 
    { name: "B", type: 1, level: 2 } 
] 
var types = [ 
    { description: "Type 1", id: 1 }, 
    { description: "Type 2", id: 2 } 
] 
var levels = [ 
    { description: "Level 1", id: 1 }, 
    { description: "Level 2", id: 1 } 
] 

있다고 가정 Observables를 중첩하지 않은 솔루션.
감사합니다.

답변

0

왜이 문제로 Rx를 사용하는지 잘 모르겠습니다. 시간 경과에 따른 데이터 (즉, 관찰 가능한 시퀀스)가 아닌 공간 (즉, 배열)에 데이터가 있습니다. 그러나 이러한 배열을 Rx로 보내면 매우 복잡한 솔루션이 생성됩니다.

소스 배열 유형에 가입하려는 대답 https://stackoverflow.com/a/17500836/393615과 같은 것을 찾고 있다고 생각합니다. 귀하의 경우에는 "내부 조인"을 두 번하여 세 가지 데이터 세트를 모두 합치십시오.

0

필터링 된 스트림의 결과와 원본 스트림의 최신 값을 결합하고 투영 함수를 사용하여 결과를 단일 개체로 병합하는 switchMap 연산자를 사용하여이 파일을 보관할 수 있습니다. 두 경우 모두 일반적인 고차 함수를 사용할 수 있도록 예제에서 일반화 할 수 있습니다. fiddle을 참조하십시오.

전체 코드 (ES2015, RxJS5) :

const users = [ 
    { name: "A", type: 2, level: 1 }, 
    { name: "B", type: 1, level: 2 } 
]; 
const types = [ 
    { description: "Type 1", id: 1 }, 
    { description: "Type 2", id: 2 } 
]; 
const levels = [ 
    { description: "Level 1", id: 1 }, 
    { description: "Level 2", id: 2 } 
]; 

const users$ = Rx.Observable.from(users); 
const types$ = Rx.Observable.from(types); 
const levels$ = Rx.Observable.from(levels); 

function join(s$, sourceProperty, targetProperty, streamProperty) { 
    return function(initObj) { 
    const stream$ = s$.filter(x => x.id === initObj[sourceProperty]); 
    return Rx.Observable.combineLatest(
     Rx.Observable.of(initObj), 
     stream$, 
     (obj, streamObj) => { 
     const prop = streamObj[streamProperty]; 
     return Object.assign({}, obj, { [targetProperty]: prop }); 
     } 
    ); 
    }; 
} 

users$ 
    .switchMap(join(types$, 'type', 'typeDescription', 'description')) 
    .switchMap(join(levels$, 'level', 'levelDescription', 'description')) 
    .subscribe(x => console.log(x)); 
관련 문제