2017-10-19 11 views
0

je frappe une API qui retourne un objet JSON qui contient des informations de prix pour divers métaux précieux, tels que le prix de l'offre, le cours vendeur, pour cent changement, haut/bas, etc.Quelle est la meilleure façon de construire un nouvel objet JS à partir d'un objet JSON existant?

Je ne ai pas besoin tous les propriétés ou tous les types de métal à l'intérieur de l'objet, et il peut y avoir des scénarios où je veux faire une boucle sur l'objet, donc j'ai pensé qu'il serait logique de reconstruire l'objet avec seulement les propriétés dont j'ai besoin. J'étais curieux de savoir quelle est la meilleure façon de le faire.

est ici l'objet qui est à moi est retourné par l'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" 
} 

Disons que je ne veux que l'ask_usd_toz, change_dollar_usd_toz et change_percent_usd_toz pour l'or, l'argent, le platine et le palladium.

Est-ce que ce qui suit est un bon moyen de construire un nouvel objet avec seulement les données dont j'ai besoin?

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 
     } 
    } 
    } 

Je pense qu'il y a une façon plus succincte de le faire. Mais peut-être pas. Je vous remercie!

+0

'JSON.parse (métal);' – zer00ne

+0

@ zer00ne Vous devriez probablement supprimer ce commentaire. – Archer

Répondre

4

Edit:

function getMyObj(obj) { 

    var types = ['gold', 'silver', 'platinum', 'palladium']; 
    var namesMap = { 
     ask_usd_toz: 'price', 
     change_dollar_usd_toz: 'changeLastClose', 
     change_percent_usd_toz: 'percentChange' 
    }; 

    var myObj = {}; 

    types.forEach(function(type) { 
     for (var prop in namesMap) { 
      var key = type + '_' + prop; 
      if (obj.hasOwnProperty(key)) { 
       myObj[type] = myObj[type] || {}; 
       myObj[type][namesMap[prop]] = obj[key]; 
      } 
     } 
    }); 

    return myObj; 
} 

getMyObj(response); 

response est votre objet JSON.

+0

Parfait. C'est exactement ce que je pensais pouvoir faire mais je ne pouvais pas le trouver moi-même. Je vous remercie. – reknirt

+1

@reknirt modifié pour une transformation conforme à votre exemple – Andrea

0

Vous pouvez prendre un objet avec les noms pour mapper un groupe par la première partie de la propriété.

var data = { 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" }, 
 
    wanted = { gold_ask_usd_toz: 'goldPrice', gold_change_dollar_usd_toz: 'goldChangeLastClose', gold_change_percent_usd_toz: 'goldPercentChange', silver_ask_usd_toz: 'silverPrice', silver_change_dollar_usd_toz: 'silverChangeLastClose', silver_change_percent_usd_toz: 'silverPercentChange', platinum_ask_usd_toz: 'platinumPrice', platinum_change_dollar_usd_toz: 'platinumChangeLastClose', platinum_change_percent_usd_toz: 'platinumPercentChange', palladium_ask_usd_toz: 'palladiumPrice', palladium_change_dollar_usd_toz: 'palladiumChangeLastClose', palladium_change_percent_usd_toz: 'palladiumPercentChange' }, 
 
    metal = {}; 
 

 
Object.keys(wanted).forEach(function (k) { 
 
    var key = k.match(/^[^_]+/)[0]; 
 
    metal[key] = metal[key] || {}; 
 
    metal[key][wanted[k]] =data[k]; 
 
}); 
 

 
console.log(metal);
.as-console-wrapper { max-height: 100% !important; top: 0; }