감사를 질문 Phosphoros을 명확히하십시오.
// Function to compare two objects by comparing their `unwrappedName` property.
const compareFn = (a, b) => {
if (a.unwrappedName < b.unwrappedName)
return -1;
if (a.unwrappedName > b.unwrappedName)
return 1;
return 0;
};
// Array of Thing objects wrapped in an observable.
// NB. The `thing.name` property is itself an observable.
const thingsObs = Observable.from([
{ id: 1, name: Observable.of('foo') },
{ id: 2, name: Observable.of('bar') },
{ id: 3, name: Observable.of('jazz') }
]);
// Now transform and subscribe to the observable.
thingsObs
// Unwrap `thing.name` for each object and store it under `thing.unwrappedName`.
.mergeMap(thing =>
thing.name.map(unwrappedName => Object.assign(thing, {unwrappedName: unwrappedName}))
)
// Gather all things in a SINGLE array to sort them.
.toArray()
// Sort the array of things by `unwrappedName`.
.map(things => things.sort(compareFn))
.subscribe();
로깅 방출 값을 콘솔에 자신의 unwrappedName
재산으로 분류 것은 객체의 배열이 표시됩니다 :
[
{ id: 2, name: ScalarObservable, unwrappedName: "bar" },
{ id: 1, name: ScalarObservable, unwrappedName: "foo" },
{ id: 3, name: ScalarObservable, unwrappedName: "jazz" }
]
주시기 바랍니다 :)를 여기
는 질문 당신이 할 수있는 방법 이 코드에 대해 궁금한 점이 있으면 알려드립니다.
Observable으로 이름을 지정하는 이유는 무엇입니까? 난 당신이 정상적인 문자열로 그 속성을두고 싶다고 생각합니다 –
내가 관찰 할 수있는 필드로 사용하고 싶습니다, 그 값은 런타임에 변경할 수 있기 때문에. – sclausen
Phosphoros, 당신은'name'이나'Thing'에 가입하지 않고, (Observable') –