2016-12-20 9 views
-1

私は以下のようなJSONを持っており、JSONのリンクから履歴と個人の詳細を取得することで、ユーザーの完全なプロファイルを持つデータ構造を作成する必要があります。どのように私は斧を使用してそれを行うにはどのような考え?Axiosは滝のようになります

JSON={ 
     0:{ 
      userid:"...", 
      details:"http:://<user>/profile", 
      history:"http://<user>/history" 
     } 
     1:{ 
      userid:"...", 
      details:"http:://<user>/profile", 
      history:"http://<user>/history" 
     } 
     2:{ 
      userid:"...", 
      details:"http:://<user>/profile", 
      history:"http://<user>/history" 
     } 
    } 

所望の結果JSON詳細および履歴キーと値のペアを仮定

JSON:{ 
    0:{ 
     userid:"1", 
     name:"<datafrom profile>", 
     age:"<data from profile>", 
     last_login:"<data from history>" 
    }, 
    1:{ 
     userid:"1", 
     name:"<datafrom profile>", 
     age:"<data from profile>", 
     last_login:"<data from history>" 
    }, 
    2:{ 
     userid:"2", 
     name:"<datafrom profile>", 
     age:"<data from profile>", 
     last_login:"<data from history>" 
    }, 
    } 

答えて

1

が合法的なURLであるです。この例ではhttp://someDataUrl.comhttp://someHistoryUrl.comを使用します。

また、エンドポイントが何を返すかわからないので、いくつか例を挙げました。

function getDataInfo(user){ 
    return axios.get(`http://someDataUrl.com/${user}`) 
} 

function getHistoryInfo(user){ 
    return axios.get(`http://someHistoryUrl.com/${user}`) 
} 

let json = {} 
axios.all([getDataInfo(), getHistoryInfo()]) 
    .then(axios.spread(function (data, history) { 
    // use response from each to populate data object literal. Something like this? : 
    json.name = data.name 
    json.last_login = history.last_login 
    })); 
+0

ありがとうアンドリュー。それは本当に私にアイデアを与えます。問題文を更新しました。 –

+0

jsonリクエストを使用して、最初の例のデータを2番目の例にマッピングしたいようですか? –

+0

はい私は2番目のjsonを作成するために最初のjsonでリンクを繰り返しフェッチする必要があります –

関連する問題