0

私は1つの同種配列にフラット化したい構造体を持っています。ソース配列は、次のようになります。私は次のようになりますにこれを変換したいと思い何rxJSの配列を平坦化またはマージする方法

[ 
    { 
    "countryCode": "CA", 
    "countryName": "Canada", 
    "states": [ 
     { 
     "stateCode": "CAAB", 
     "stateName": "Alberta", 
     "countryCode": "CA", 
     "stateAbbrev": "AB" 
     }, 
     . . . 
     { 
     "stateCode": "CAYT", 
     "stateName": "Yukon Territory", 
     "countryCode": "CA", 
     "stateAbbrev": "YT" 
     } 
    ] 
    }, 
    { 
    "countryCode": "US", 
    "countryName": "USA", 
    "states": [ 
     { 
     "stateCode": "USAK", 
     "stateName": "Alaska", 
     "countryCode": "US", 
     "stateAbbrev": "AK" 
     }, 
     . . . 
     { 
     "stateCode": "USWY", 
     "stateName": "Wyoming", 
     "countryCode": "US", 
     "stateAbbrev": "WY" 
     } 
    ] 
    } 
] 

[ 
    { 
    "value": "CA", 
    "label": "Canada" 
    }, 
    { 
    "value": "CACB", 
    "label": "Alberta" 
    }, 
    . . . 
    { 
    "value": "CAYT", 
    "label": "Yukon Territory" 
    }, 
    { 
    "value": "US", 
    "label": "USA" 
    }, 
    { 
    "value": "USAK", 
    "label": "Alaska" 
    }, 
    . . . 
    { 
    "value": "USWY", 
    "label": "Wyoming" 
    } 
] 

は、これまでのところ私が持っている:

let countries:Observable<ICountry[]> = 
    this.http.get<ICountry[]>(`${this.buildProUrl}/states`); 

return countries.map(o => o.map(c => 
    <IStateDropDownItem>{value: c.countryCode, label: c.countryName})); 

それはそこのように思えますそれぞれの国に属する州を結果として得られる観測可能な配列にマージする方法でなければならない。私はconcatMap、mergeMap、switchMapのドキュメントを読んだことがありますが、すべてをまとめておく方法を理解することはできません。

+0

私が正しく理解していれば、これはRxJSとは関係ありません。 *ストリーム*をマージしたくない場合は、ストリーム内のオブジェクトを変更したいだけです。したがって、RxJSのドキュメントはまったく役に立ちません。バニラのTS/JS関数を記述する必要があります。この関数は入力構造体を取り出し、出力構造体を返し、ストリームに '.map'を返します。 – jonrsharpe

+0

これはRxJSとは関係ありません。国の配列をIStateDropDownItemの配列に変換したいだけです。ですから、すでに使用している.map()演算子が必要です。国の配列をIStateDropDownItemsの配列に変換する関数を渡す必要があります。 –

+0

おそらく、2つの異なるマップを使用していることが混乱しているかもしれません。 'countries.map'はObservable.mapですが、' o.map'は 'Array.map'です。 – jonrsharpe

答えて

1

私はあなただけArry.reduce()機能を使用して行うことができ、あなたの結果の配列を処理する必要があると思う:

const data = [ 
 
    { 
 
    "countryCode": "CA", 
 
    "countryName": "Canada", 
 
    "states": [ 
 
     { 
 
     "stateCode": "CAAB", 
 
     "stateName": "Alberta", 
 
     "countryCode": "CA", 
 
     "stateAbbrev": "AB" 
 
     }, 
 
     { 
 
     "stateCode": "CAYT", 
 
     "stateName": "Yukon Territory", 
 
     "countryCode": "CA", 
 
     "stateAbbrev": "YT" 
 
     } 
 
    ] 
 
    }, 
 
    { 
 
    "countryCode": "US", 
 
    "countryName": "USA", 
 
    "states": [ 
 
     { 
 
     "stateCode": "USAK", 
 
     "stateName": "Alaska", 
 
     "countryCode": "US", 
 
     "stateAbbrev": "AK" 
 
     }, 
 
     { 
 
     "stateCode": "USWY", 
 
     "stateName": "Wyoming", 
 
     "countryCode": "US", 
 
     "stateAbbrev": "WY" 
 
     } 
 
    ] 
 
    } 
 
]; 
 

 
console.log(data.reduce((res, curr) => { 
 
    res.push({value: curr.countryCode, label: curr.countryName}); 
 
    return res.concat(curr.states.reduce((res, curr) => { 
 
    res.push({value: curr.stateCode, label: curr.stateName}); 
 
    return res; 
 
    }, [])); 
 
}, []));

新しいのHttpClientを使用している場合は、あなたの回答が既に配列でありますあなたのケースではうまくいくはずです:

let countries:Observable<ICountry[]> = 
    this.http.get<ICountry[]>(`${this.buildProUrl}/states`); 

return countries.map(o => o.reduce((res, curr) => { 
    res.push(<IStateDropDownItem>{value: curr.countryCode, label: curr.countryName}); 
    return res.concat(curr.states.reduce((res, curr) => { 
    res.push(<IStateDropDownItem>{value: curr.stateCode, label: curr.stateName}); 
    return res; 
    }, [])); 
}, [])); 
+0

downvotingを説明してください – Andriy

+0

ありがとう!それがトリックでした。チャンピオンのように動作します。 – Colin

関連する問題