循环处理复杂的JSON对象-需要逻辑来获取值的总和

时间:2019-04-09 04:23:35

标签: json algorithm typescript logic

我在Angular应用程序中的JENKINS API下使用了JSON。我正在尝试从下面的JSON获取键public static double roundToDigits(double value, int digitCount) { if (digitCount < 0) throw new IllegalArgumentException("Digit count must be positive for rounding!"); double factor = Math.pow(10, digitCount); return (double)(Math.round(value * factor)) / factor; } 的值的总和。我需要在打字稿中实施的逻辑。

问题就在这里,JSON结构不断变化。如何编写逻辑来计算键“ n”的值之和

下面是JSON结构的示例:

n

总和= 18(3 + 6 + 9)

示例:2

{
   "J": [
          {
             "n" : 3,
             "J" : [
                    {
                      "n": 6
                      "J": [
                             {
                             "n": 9
                              }
                           ]
                     }
                   ]
          }
        ]
}

sum = 9

2 个答案:

答案 0 :(得分:0)

不需要花哨的逻辑,只需简单地递归Array.reduce()即可:

var sumOfN = function(o) {  
  return (o['J'] || []).reduce((m,x) => { 
    return m + sumOfN(x); 
  }, (o['n'] || 0));
};

示例1:

var data = {
   "J": [
          {
             "n" : 3,
             "J" : [
                    {
                      "n": 6,
                      "J": [
                             {
                             "n": 9
                              }
                           ]
                     }
                   ]
          }
        ]
};

var sumOfN = function(o) {  
  return (o['J'] || []).reduce((m,x) => { 
 	  return m + sumOfN(x); 
  }, (o['n'] || 0));
};

console.log('result = ' + sumOfN(data));

示例2:

var data = {
  "J": [{
    "J": [{
      "J": [{
        "J": [{
          "J": [{
            "n": 9
          }]
        }]
      }]
    }]
  }]
};

var sumOfN = function(o) {  
  return (o['J'] || []).reduce((m,x) => { 
 	  return m + sumOfN(x); 
  }, (o['n'] || 0));
};

console.log('result = ' + sumOfN(data));


更新

示例#3

var data = { "J": [{ "J": [ { "J": [{ "J": [{ "J": [{ "n": 9 }] }] }] }, { "J": [{ "n": 9 }, { "n": 9 } ] }	] }] };

var sumOfN = function(o) {  
  return (o['J'] || []).reduce((m,x) => { 
 	  return m + sumOfN(x); 
  }, (o['n'] || 0));
};

console.log('result = ' + sumOfN(data));

答案 1 :(得分:0)

这是递归TypeScript解决方案:

type J = {
    n?: number,
    J?: J[]
}

function sum(j: J, acc = 0) {
    if (j.J != null) {
        for (let subJ of j.J) {
            acc = sum(subJ, acc);
        }
    }
    return acc + (j.n ? j.n : 0);
}


console.log(sum({
    "J": [
        {
            "n": 3,
            "J": [
                {
                    "n": 6,
                    "J": [
                        {
                            "n": 9
                        }
                    ]
                }
            ]
        }
    ]
})); // 18

console.log(sum({
    "J": [
        {

            "J": [
                {

                    "J": [
                        {
                            "J": [
                                {
                                    "J": [
                                        {
                                            "n": 9
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
})); // 9