在条件下从JSON收集值

时间:2017-11-10 03:55:32

标签: javascript json google-apps-script

我正在努力在条​​件下从JSON收集值。基础数据和预期数据如下。我想收集与key1和key2的值相同的ID。收集数据的关键是当每个元素的key1和key2相同时,我想得到每个键的值。我很抱歉这个令人困惑的解释。

我必须在Google Apps脚本中使用此脚本。所以请注意我不能使用ECMAScript6。

基础JSON

var data = [
    {id: "id_A", key1: "value1", key2: "value2"},
    {id: "id_B", key1: "value2", key2: "value3"},
    {id: "id_C", key1: "value3", key2: "value4"},
    {id: "id_D", key1: "value2", key2: "value3"},
    {id: "id_E", key1: "value2", key2: "value3"},
    {id: "id_F", key1: "value3", key2: "value4"},
    {id: "id_G", key1: "value1", key2: "value2"},
    {id: "id_H", key1: "value4", key2: "value5"}
]

预期结果

var result = [
    {id: ["id_A", "id_G"], key: ["value1", "value2"]},
    {id: ["id_B", "id_D", "id_E"], key: ["value1", "value2", "value3"]},
    {id: ["id_C", "id_F"], key: ["value1", "value2", "value3", "value4"]},
    {id: ["id_H"], key: ["value1", "value2", "value3", "value4", "value5"]}
]

我当前的剧本

function convertJSON(){
  var json = [
      {id: "id_A", key1: "value1", key2: "value2"},
      {id: "id_B", key1: "value2", key2: "value3"},
      {id: "id_C", key1: "value3", key2: "value4"},
      {id: "id_D", key1: "value2", key2: "value3"},
      {id: "id_E", key1: "value2", key2: "value3"},
      {id: "id_F", key1: "value3", key2: "value4"},
      {id: "id_G", key1: "value1", key2: "value2"},
      {id: "id_H", key1: "value4", key2: "value5"}
  ];

  var sorted = json.sort(function(i, j){
    if (i.key1 < j.key1){
      return -1;
    }
    if (i.key1 > j.key1){
      return 1;
    }
    return 0;
  });

  var result = [];
  var id = [];
  var key = [];
  var key1 = "";
  var key2 = "";
  for (var i = 0; i < sorted.length; i++){ 
    if (i > 0) {
      if (key1 == sorted[i].key1 && key2 == sorted[i].key2){
        id.push(sorted[i].id);
        key.push(sorted[i].key1);
        key.push(sorted[i].key2);
      } else {
        var key = key.filter(function(e, i, ar) {
          return ar.indexOf(e) == i;
        });
        result.push({id: id, key: key});
        var id = [];
        var key = [];    
        var key1 = sorted[i].key1;
        var key2 = sorted[i].key2;
        id.push(sorted[i].id);
        key.push(sorted[i].key1);
        key.push(sorted[i].key2);
      }
    } else {
      var key1 = sorted[i].key1;
      var key2 = sorted[i].key2;
      id.push(sorted[i].id);
      key.push(sorted[i].key1);
      key.push(sorted[i].key2);
    }
  }
  console.log(JSON.stringify(result));

// result is
// [
//   {"id":["id_A","id_G"],"key":["value1","value2"]},
//   {"id":["id_B","id_D","id_E"],"key":["value2","value3"]},
//   {"id":["id_C","id_F"],"key":["value3","value4"]}
// ]
}

非常感谢您的时间和建议。

1 个答案:

答案 0 :(得分:1)

这个示例脚本怎么样?请将此视为几个答案之一。我认为有更有效的方法。当我看到你的问题时,起初我想到了3个部分。所以我分成3个部分来检索结果。

  1. 检索第一步和最后一步。
    • 检索的第一步和最后一步用作分析楼梯的参考点。
  2. Analize stairs。
    • 按顺序检索整个步骤。
  3. 汇总ID和密钥。
  4. 我确认此示例脚本适用于GAS。

    示例脚本:

    var data = [
        {id: "id_A", key1: "value1", key2: "value2"},
        {id: "id_B", key1: "value2", key2: "value3"},
        {id: "id_C", key1: "value3", key2: "value4"},
        {id: "id_D", key1: "value2", key2: "value3"},
        {id: "id_E", key1: "value2", key2: "value3"},
        {id: "id_F", key1: "value3", key2: "value4"},
        {id: "id_G", key1: "value1", key2: "value2"},
        {id: "id_H", key1: "value4", key2: "value5"},
    ];
    
    // 1. Retrieve first and last steps.
    var firststep, laststep;
    for (var i in data) {
      var chk1 = data[i].key1;
      var chk2 = data[i].key2;
      var f1 = true;
      var f2 = true;
      for (var j in data) {
        if (data[j].key2 == chk1) f1 = false;
        if (data[j].key1 == chk2) f2 = false;
      }
      if (f1) firststep = data[i];
      if (f2) laststep = data[i];
    }
    
    // 2. Analize stairs.
    var stairs = [];
    stairs.push(firststep);
    var key1 = firststep.key1;
    var key2 = firststep.key2;
    while (key2 != laststep.key2) {
      var temp = {};
      for (var i in data) {
        if (data[i].key1 == key2) {
          if (!temp[data[i].key1]) {
            stairs.push(data[i]);
            temp[data[i].key1] = true;
          }
        }
      }
      key2 = stairs[stairs.length - 1].key2;
    }
    
    // 3. Summarize IDs and Keys.
    var result = [];
    var key = [];
    for (var i in stairs) {
      var id = [];
      if (!~key.indexOf(stairs[i].key1)) key.push(stairs[i].key1);
      key.push(stairs[i].key2);
      for (var j in data) {
        if (data[j].key1 == stairs[i].key1 && data[j].key2 == stairs[i].key2) {
          id.push(data[j].id);
        }
      }
      result.push({id: id.slice(), key: key.slice()});
    }
    
    // When you use GAS, you can see the result at Stackdriver.
    // Or please use Logger.log(JSON.stringify(result, null, "\t"))
    console.log(JSON.stringify(result, null, "\t"));

    我的理解是否正确?如果我误解了你的问题,请告诉我。我想修改。