私は、入札価格、掘り出し価格、パーセント変化、高低などのさまざまな貴金属の価格情報を持つjsonオブジェクトを返すAPIを打っています。既存のJSONオブジェクトから新しいJSオブジェクトを構築する最善の方法は何ですか?
私はすべてを必要としません。プロパティ、またはオブジェクト内のすべてのメタルタイプを使用することができます。オブジェクトをループするシナリオがあるため、必要なプロパティのみでオブジェクトを再構築することが理にかなっていると思います。私はこれを行う最善の方法が何か不思議でした。ここで
は、APIによって私に返されるオブジェクトです。
{
"gold_bid_usd_toz": "1286.32",
"gold_ask_usd_toz": "1287.32",
"gold_change_dollar_usd_toz": "5.72",
"gold_change_percent_usd_toz": "0.44%",
"gold_high_usd_toz": "1290.07",
"gold_low_usd_toz": "1277.01",
"gold_londonfix_am": "1278.44",
"gold_londonfix_pm": "1274.48",
"silver_bid_usd_toz": "17.2",
"silver_ask_usd_toz": "17.3",
"silver_change_dollar_usd_toz": "0.2",
"silver_change_percent_usd_toz": "1.17%",
"silver_high_usd_toz": "17.25",
"silver_low_usd_toz": "16.92",
"silver_londonfix": "16.69",
"platinum_bid_usd_toz": "924.75",
"platinum_ask_usd_toz": "929.75",
"platinum_change_dollar_usd_toz": "0.78",
"platinum_change_percent_usd_toz": "0.08%",
"platinum_high_usd_toz": "930.74",
"platinum_low_usd_toz": "920.5",
"platinum_londonfix_am": "914.03",
"platinum_londonfix_pm": "912.09",
"palladium_bid_usd_toz": "958.94",
"palladium_ask_usd_toz": "963.94",
"palladium_change_dollar_usd_toz": "-2.49",
"palladium_change_percent_usd_toz": "-0.26%",
"palladium_high_usd_toz": "967.31",
"palladium_low_usd_toz": "951.88",
"palladium_londonfix_am": "919.07",
"palladium_londonfix_pm": "919.01",
"englehard_fabricated_gold": "1199.21",
"englehard_fabricated_silver": "17.7000",
"usdcad": "1.248698",
"usdcny": "6.612501",
"xpt_xpd_timestamp": "1507075200000"
}
はのは私だけで金、銀、プラチナ、パラジウムのためask_usd_toz
、change_dollar_usd_toz
とchange_percent_usd_toz
をしたいとしましょう。
必要なデータだけで新しいオブジェクトを作成するには、次の方法が適していますか?
destructureObject(metal) {
const goldPrice = metal.gold_ask_usd_toz;
const goldChangeLastClose = metal.gold_change_dollar_usd_toz;
const goldPercentChange = metal.gold_change_percent_usd_toz;
const silverPrice = metal.silver_ask_usd_toz;
const silverChangeLastClose = metal.silver_change_dollar_usd_toz;
const silverPercentChange = metal.silver_change_percent_usd_toz;
const platinumPrice = metal.platinum_ask_usd_toz;
const platinumChangeLastClose = metal.platinum_change_dollar_usd_toz;
const platinumPercentChange = metal.platinum_change_percent_usd_toz;
const palladiumPrice = metal.palladium_ask_usd_toz;
const palladiumChangeLastClose = metal.palladium_change_dollar_usd_toz;
const palladiumPercentChange = metal.palladium_change_percent_usd_toz;
const metal = {
gold: {
goldPrice: goldPrice,
goldChangeLastClose: goldChangeLastClose,
goldPercentChange: goldPercentChange
},
silver: {
silverPrice: silverPrice,
silverChangeLastClose: silverChangeLastClose,
silverPercentChange: silverPercentChange
},
platinum: {
platinumPrice: platinumPrice,
platinumChangeLastClose: platinumChangeLastClose,
platinumPercentChange: platinumPercentChange
},
palladium: {
palladiumPrice: palladiumPrice,
palladiumChangeLastClose: palladiumChangeLastClose,
palladiumPercentChange: palladiumPercentChange
}
}
}
これを行うためのより簡潔な方法があるように感じます。しかし、そうでないかもしれない。ありがとうございました!
'JSON.parse(金属);あなたはおそらく、そのコメントを削除する必要がありますzer00ne @' – zer00ne
。 – Archer