简体   繁体   English

使用Javascript根据键值转换JSON

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

I get some records from a database in the following JSON format: 我从数据库获取以下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"
  }
]

I would like to transform the above result and group the y , x and geneName name/value pairs based on the cond key using JavaScript . 我想转换上述结果,并使用JavaScript根据cond键将yxgeneName名称/值对geneName

The result I'd like is shown below: 我想要的结果如下所示:

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

For every different grouping I'd like to add an extra name/value pair color . 对于每个不同的分组,我想添加一个额外的名称/值对color Is there any smart and quick way to do it using JavaScript by avoiding the naive approach of the for loops? 通过避免for循环的幼稚方法,有没有使用JavaScript的聪明快捷的方法?

Thanks in advance 提前致谢

You could use Array.prototype.reduce to group common objects by object.cond, like so: 您可以使用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> 

You can use .reduce here. 您可以在此处使用.reduce Make a object whose key is basically cond and then from that object transform a array using .map . 制作一个其键基本上是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