使用Javascript根据键值转换JSON

时间:2018-08-16 16:35:51

标签: javascript jquery json transformation

我从数据库中以以下JSON格式获取了一些记录:

data: [{
    "y": "0.652008685",
    "x": "-0.13926217",
    "geneName": "ADAMTS2",
    "cond": "Cell"
  },
  {
    "y": "-3.486001",
    "x": "-2.295312",
    "geneName": "IGSF22",
    "cond": "ECM"
  },
  {
    "y": "-3.597706",
    "x": "-2.672138",
    "geneName": "OXA1L",
    "cond": "ECM"
  }
]

我想转换以上结果,并使用y根据x键将geneNamecondJavaScript名称/值对分组

我想要的结果如下所示:

series: [{
    name: 'Cell',
    color: '#fff',
    data: [{
      "name": "ADAMTS2",
      "x": -0.13926217,
      "y": 0.652008685
    }]
  },
  {
    name: 'ECM',
    color: '#000',
    data: [{
        "name": "IGSF22",
        "x": -2.295312,
        "y": -3.486001
      },
      {
        "name": "OXA1L",
        "x": -2.672138,
        "y": -3.597706
      }
    ]
  }
]

对于每个不同的分组,我想添加一个额外的名称/值对color。 通过避免使用for循环的幼稚方法,有没有使用JavaScript的聪明快捷的方法?

预先感谢

3 个答案:

答案 0 :(得分:2)

您可以使用Array.prototype.reduce按object.cond对常见对象进行分组,如下所示:

var data = [{
    "y": "0.652008685",
    "x": "-0.13926217",
    "geneName": "ADAMTS2",
    "cond": "Cell"
  },
  {
    "y": "-3.486001",
    "x": "-2.295312",
    "geneName": "IGSF22",
    "cond": "ECM"
  },
  {
    "y": "-3.597706",
    "x": "-2.672138",
    "geneName": "OXA1L",
    "cond": "ECM"
  }
];

var dataMap = data.reduce((result, item) => {
  // create root-level object for a name if it doesn't already exist
  if (!result[item.cond]) {
    result[item.cond] = {
      name: item.cond,
      color: ''/*not sure what your logic is here*/,
      data: []
    }
  }
  
  // add current item to the root-level object data
  result[item.cond].data.push({
      name: item.geneName,
      x: parseFloat(item.x),
      y: parseFloat(item.y)
  });
  
  return result;
}, {/*resulting map*/});

// last step is to get an array of the values since that's the desired format
data = Object.values(dataMap);

document.getElementById('result').innerHTML = JSON.stringify(data, null, 4);
<pre id="result"></pre>

答案 1 :(得分:1)

您可以在此处使用.reduce。制作一个键基本上是cond的对象,然后使用.map从该对象转换数组。

var x = {
  data: [{
      "y": "0.652008685",
      "x": "-0.13926217",
      "geneName": "ADAMTS2",
      "cond": "Cell"
    },
    {
      "y": "-3.486001",
      "x": "-2.295312",
      "geneName": "IGSF22",
      "cond": "ECM"
    },
    {
      "y": "-3.597706",
      "x": "-2.672138",
      "geneName": "OXA1L",
      "cond": "ECM"
    }
  ]
};

x.data = Object.entries(x.data.reduce((acc, el) => {
  let cond = el.cond;
  delete el.cond;
  
  if(acc.hasOwnProperty(cond)){
    acc[cond].data.push(el);
  }
  else{
    acc[cond] = {};
    acc[cond].data = [el];
  }
  
  return acc;
  
}, {})).map(el => {
  return {name: el[0], data: el[1].data};
});

console.log(x);

答案 2 :(得分:0)

data = [{
"y": "0.652008685",
"x": "-0.13926217",
"geneName": "ADAMTS2",
"cond": "Cell"
  },
  {
    "y": "-3.486001",
    "x": "-2.295312",
    "geneName": "IGSF22",
    "cond": "ECM"
  },
  {
    "y": "-3.597706",
    "x": "-2.672138",
    "geneName": "OXA1L",
    "cond": "ECM"
  }
]

series = []
definedName = []

data.forEach(function(item) {
  var ind = definedName.findIndex((element) => {
    return element == item.cond;
  });
  if (ind === -1) {
    obj = {
      name: item.cond,
      color: '#fff',
      data: [{
        "name": item.geneName,
        "x": item.x,
        "y": item.y
      }]
    }
    definedName.push(item.cond)
    series.push(obj)
  } else {
    obj = {
      "name": item.geneName,
      "x": item.x,
      "y": item.y
    }
    series[ind]["data"].push(obj)
  }
});

console.log(series)