如何在对象中推送值

时间:2018-01-12 06:01:57

标签: javascript node.js

这是多维对象:

  

var a = {} a.push({hello:'abvc'})

不工作

3 个答案:

答案 0 :(得分:1)

您可以使用Object.assign()array#map解构sytax。



var data = { "BTC": { "FRC": { "lowest_ask": "793650.7936508", "highest_bid": "335570.46979865", "last_trade": "540540.54054054", "volume": "0.0" }, "LTC": { "lowest_ask": "59.99995201", "highest_bid": "54.34782608", "last_trade": "54.34782608", "volume": "0.00200782" }, "NMC": { "lowest_ask": "3950.22713807", "highest_bid": "2632.27165043", "last_trade": "2631.57894736", "volume": "0.0" }, "PPC": { "lowest_ask": "2857.14285715", "highest_bid": "2222.22222222", "last_trade": "2380.95238095", "volume": "0.0" } }, "DASH": { "USD": { "lowest_ask": "0.0", "highest_bid": "0.0", "last_trade": "0.0", "volume": "0.0" }, "XPM": { "lowest_ask": "0.0", "highest_bid": "0.0", "last_trade": "0.0", "volume": "0.0" } } },
    result = Object.assign(...Object.keys(data).map(k1 => ({[k1] : Object.assign(...Object.keys(data[k1]).map(k2 => ({[k2]: {}})))})));
console.log(result);




您还可以将array#reduceObject.keys()

一起使用



var data = { "BTC": { "FRC": { "lowest_ask": "793650.7936508", "highest_bid": "335570.46979865", "last_trade": "540540.54054054", "volume": "0.0" }, "LTC": { "lowest_ask": "59.99995201", "highest_bid": "54.34782608", "last_trade": "54.34782608", "volume": "0.00200782" }, "NMC": { "lowest_ask": "3950.22713807", "highest_bid": "2632.27165043", "last_trade": "2631.57894736", "volume": "0.0" }, "PPC": { "lowest_ask": "2857.14285715", "highest_bid": "2222.22222222", "last_trade": "2380.95238095", "volume": "0.0" } }, "DASH": { "USD": { "lowest_ask": "0.0", "highest_bid": "0.0", "last_trade": "0.0", "volume": "0.0" }, "XPM": { "lowest_ask": "0.0", "highest_bid": "0.0", "last_trade": "0.0", "volume": "0.0" } } },
    result = Object
              .keys(data)
              .reduce((r, k1) => (r[k1] = Object
                          .keys(data[k1])
                          .reduce((r2,k2) => 
                            (r2[k2] = {}, r2), {})
                 , r), {});
console.log(result);




答案 1 :(得分:0)

你有两个级别..所以建立一个新的属性..

var input ={
    "BTC": {
        "FRC": {
            "lowest_ask": "793650.7936508",
            "highest_bid": "335570.46979865",
            "last_trade": "540540.54054054",
            "volume": "0.0"
        },
        "LTC": {
            "lowest_ask": "59.99995201",
            "highest_bid": "54.34782608",
            "last_trade": "54.34782608",
            "volume": "0.00200782"
        },
        "NMC": {
            "lowest_ask": "3950.22713807",
            "highest_bid": "2632.27165043",
            "last_trade": "2631.57894736",
            "volume": "0.0"
        },
        "PPC": {
            "lowest_ask": "2857.14285715",
            "highest_bid": "2222.22222222",
            "last_trade": "2380.95238095",
            "volume": "0.0"
        }
    },
    "DASH": {
        "USD": {
            "lowest_ask": "0.0",
            "highest_bid": "0.0",
            "last_trade": "0.0",
            "volume": "0.0"
        },
        "XPM": {
            "lowest_ask": "0.0",
            "highest_bid": "0.0",
            "last_trade": "0.0",
            "volume": "0.0"
        }
    }
};

var result = {};
for (var x in input){
  result[x] = {};
  for (var y in input[x]){
    result[x][y] = {};
  }
}

console.log(JSON.stringify(result));

答案 2 :(得分:0)

  function keyOnly(obj, result = {}){
    for(var [key, value] of Object.entries(obj)){
       if(typeof value === "object"){
           keyOnly(value, result[key] = {});
       }
   }
   return result;
}

可能使用递归并仅接管对象属性。