从现有JSON对象构建新JS对象的最佳方法是什么?

时间:2017-10-19 18:56:25

标签: javascript json javascript-objects

我正在点击一个返回json对象的API,该对象包含各种贵金属的价格信息,例如出价,要价,变化百分比,高/低等。

我不需要对象内部的所有属性或所有金属类型,并且可能存在我想要遍历对象的情况,所以我认为仅使用属性重建对象可能是有意义的我需要。我很好奇最好的办法是什么。

以下是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_tozchange_dollar_usd_tozchange_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
      }
    }
  }

我觉得有一种更简洁的方法可以做到这一点。但也许不是。谢谢!

2 个答案:

答案 0 :(得分:4)

修改

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是你的json对象。

答案 1 :(得分:0)

您可以使用名称来创建一个对象,以便按属性的第一部分映射组。



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