繁体   English   中英

使用Javascript根据键值转换JSON

[英]Convert JSON based on a key value using Javascript

我从数据库获取以下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"
  }
]

我想转换上述结果,并使用JavaScript根据cond键将yxgeneName名称/值对geneName

我想要的结果如下所示:

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的聪明快捷的方法?

提前致谢

您可以使用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> 

您可以在此处使用.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); 

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM