【问题标题】:Create Multiple dictionary from single single JSON response in javascript?从javascript中的单个JSON响应创建多个字典?
【发布时间】:2015-11-27 15:28:58
【问题描述】:

假设我有一个来自服务器的 JSON 响应,其结构如下

   var data={
    "Data1": {
      "height": 39, 
      "weight": 62, 
      "shape": {
        "length": 19, 
        "width": 72
      }, 
      "color": "#00ff00", 
      "radius": 9.5, 
      "color_srv": "#ffff00"
    }, 
    "Data2": {
      "height": 0, 
      "weight": 40, 
      "shape": {
        "length": 19, 
        "width": 72
      }, 
      "color": "#000000", 
      "radius": 2.5, 
      "color_srv": "#ff0000"
    }
  }

我希望这个数据字典在保持结构的同时将某些数据分成两个字典。例如

  var data_height = {
    "Data1":{
      "height": 39,  
      "shape": {
        "length": 19, 
        "width": 72
      }, 
      "color": "#00ff00", 
      "radius": 9.5, 
    },
    "Data2":{
      "height": 0,  
      "shape": {
        "length": 19, 
        "width": 72
      }, 
      "color": "#000000", 
      "radius": 2.5, 
    }
  }
  var data_weight = {
    "Data1":{
      "weight": 39,  
      "shape": {
        "length": 19, 
        "width": 72
      }, 
      "color_srv": "#00ff00", 
      "radius": 9.5, 
    },
    "Data2":{
      "weight": 0,  
      "shape": {
        "length": 19, 
        "width": 72
      }, 
      "color_srv": "#000000", 
      "radius": 2.5, 
    }
  }

以上两个字典的用途不同,所以在得到统一结果后,我想如何将后端的单个数据拆分为两个不同的字典。

编辑

这是我尝试做的事情,但它会引发错误

解决方案1:

    var serve={},live={};
      for(d in data){
        pname = d.split(':')[0];
        serve['pname'].radius= data[d].radius;
        serve['pname'].center= data[d].center;
        serve['pname'].color= data[d].color_srv;
        live['pname'].radius= data[d].radius;
        live['pname'].center= data[d].center;
        live['pname'].color= data[d].color;
        serve['pname'].numbers= data[d].serving;
        live['pname'].numbers= data[d].living;
        serve['pname'].place= pname;
        live['pname'].place= pname;
      }

编辑2

解决方案 2:

  var serve={},live={};
    for(d in data){
      pname = d.split(':')[0];
      serve['radius']= data[d].radius;
      serve['center']= data[d].center;
      serve['color']= data[d].color_srv;
      live['radius']= data[d].radius;
      live['center']= data[d].center;
      live['color']= data[d].color;
      serve['numbers']= data[d].serving;
      live['numbers']= data[d].living;
      serve['place']= pname;
      live['plcae']= pname;
    }

上述两种解决方案似乎都不起作用。

【问题讨论】:

  • 您需要将您已经尝试过的代码添加到您的问题中。我们希望看到您首先付出了努力。
  • @Andy 这是我试过的。 l
  • 字符串化和解析并分配给新变量,然后,如果真的需要,删除不需要的属性。

标签: javascript json ajax dictionary


【解决方案1】:

正如 Nina 所说,只需克隆对象并从每个对象中删除不需要的属性。在这里,我使用了 reduce 和具有 data_heightdata_height 属性的初始对象。

var clone = function (obj) { return JSON.parse(JSON.stringify(obj)); }

var output = Object.keys(data).reduce(function (p, c) {
  var obj = data[c];
  p.data_height[c] = clone(obj);
  delete p.data_height[c].weight;
  delete p.data_height[c].color_srv;
  p.data_weight[c] = clone(obj);
  delete p.data_weight[c].height;
  delete p.data_weight[c].color;
  return p;
}, { data_height: {}, data_weight: {} });

输出

{
  "data_height": {
    "Data1": {
      "height": 39,
      "shape": {
        "length": 19,
        "width": 72
      },
      "color": "#00ff00",
      "radius": 9.5
    },
    "Data2": {
      "height": 0,
      "shape": {
        "length": 19,
        "width": 72
      },
      "color": "#000000",
      "radius": 2.5
    }
  },
  "data_weight": {
    "Data1": {
      "weight": 62,
      "shape": {
        "length": 19,
        "width": 72
      },
      "radius": 9.5,
      "color_srv": "#ffff00"
    },
    "Data2": {
      "weight": 40,
      "shape": {
        "length": 19,
        "width": 72
      },
      "radius": 2.5,
      "color_srv": "#ff0000"
    }
  }
}

DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    相关资源
    最近更新 更多