2016-09-06 4 views
0

안녕하세요, 저는 서버에서 오는 두 개의 배열을 가지고 있고 배열의 첫 번째 배열을 기준으로 2 개의 값을 할당해야합니다. 첫 번째 배열의 예제 나는이 ID를 기반으로 실제 이름을 찾고 할당해야하는 사람의 nameId를가집니다. 그것의 정확한 reactWay 그것을하기 위하여 확실하지 않다;다른 관측 가능을 기반으로 개체에 속성 추가

var names = this.nameService.query() 
     .flatMap((names) => { 
      return Observable.from(names); 
     }).flatMap(name => { 
      return Observable.combineLatest(Observable.of(name), this.nameArray, (name,nameArray) => { 
       name.realName = this.nameService.getRealNamedName(name.nameId, names); 
       return name; 
      }) 
     }).toArray(); 


names.subscribe(names => { 
this.names = names 
}) 

편집 :

필수적 프로그래밍

내가 그것을이

var promises = { 
      people: peopleService.getPeople(), 
      names: nameService.getNames() 
     }; 

    $q.all(promises).then(result => { 
     this.people = result.people.forEach(person => { 
          person.realName = result.names.find((name) => { 
           return name.id === person.id 
          })['realName'] 
         }) 
    }) 

답변

0

업데이트처럼 쓴다은 :

const realNames = [ 
    {'id': 2, 'realName': 'jane'}, 
    {'id': 1, 'realName': 'john'}, 
    {'id': 3, 'realName': 'joe'} 
]; 

const peopleObservable = Rx.Observable.from([ 
    {id: 3, foo: 'foo3', bar: 'bar3'}, 
    {id: 1, foo: 'foo1', bar: 'bar1'}, 
    {id: 2, foo: 'foo2', bar: 'bar2'} 
]); 

const justNamesObservable = Rx.Observable.just(realNames); 
const peopleWithNameObservable = Rx.Observable.combineLatest(peopleObservable, justNamesObservable); 

var subscription = peopleWithNameObservable.subscribe(
    x => { 
    const person = x[0]; 
    const names = x[1]; 
    const personWithRealName = names.find((name) => { 
     return name.id === person.id 
    });   
    person.realName = personWithRealName.realName   
    console.log(person); 
    }, 
    e => { 
    console.log(e); 
    }, 
() => { 
    console.log('complete'); 
    } 
); 

당신은 Observable.combineLatest 사용할 수 있습니다. 트릭은 신호가 방출 될 때마다 사람 개체와 전체 이름 배열을 포함하며 여기에서 find()를 수행 할 수 있습니다.

http://jsbin.com/yurufi/edit?js,console


이전 답 :

내가 (이 답변의 시간에) 너무 요구 사항에 분명하지만, 당신이 게시 귀하의 질문에 따라 아니에요, 나는이 가정하고 배열은 같은 순서로 방출하고있다되어 1 대 1, 당신은 다음과 Observable.zip 사용할 수 있습니다

const getRealNamedName = id => { 
    const realNames = { 
    '1': 'john', 
    '2': 'jane', 
    '3': 'joe', 
    }; 
    return realNames[id]; 
} 

const firstArray = Rx.Observable.from([1, 2, 3]); 
const secondArray = Rx.Observable.from([ 
    {id: 1, foo: 'foo1', bar: 'bar1'}, 
    {id: 2, foo: 'foo2', bar: 'bar2'}, 
    {id: 3, foo: 'foo3', bar: 'bar3'} 
]); 

const zipped = Rx.Observable.zip(firstArray, secondArray, (id, second) => 
    Object.assign({'realName': getRealNamedName(id)}, second) 
); 

zipped.subscribe(name => 
    { 
    console.log(name); 
    }, 
    e => { 
    //handle error 
    }, 
() => { 
    //handle complete 
    } 
); 

http://jsbin.com/jiqofihoko/edit?js,console

+0

고맙습니다.하지만 제가 찾고 있던 것이 더 명확하게하기위한 질문을 업데이트 할 것입니다. – alexKhymenko

+0

@alexKhymenko 답변을 –

+0

업데이트했습니다. – alexKhymenko

관련 문제