通过匹配ID组合JSON数组的值-

时间:2019-09-08 16:27:37

标签: javascript json

我正在尝试组合JSON对象的值以显示发往特定目标的流量的完整吞吐量,而不是显示到同一地址的微小值。

我已经阅读了数百个线程,它们似乎都围绕着使用单独的键或将值组合到单个字段中将不同数组中的值添加到一个中来。

这里的关键是IP地址,要添加的值是RX和TX

我的数据如下:

[
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.172.63.97", "tx":500, "rx":750},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.189.157.90", "tx":1000, "rx":2776},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.212.176.21", "tx":70, "rx":300},
]

我想将其转换为:

[
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx": 200, "rx": 400},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.172.63.97", "tx": 500, "rx": 750},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.189.157.90", "tx": 1000, "rx": 2776},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.212.176.21", "tx": 70, "rx": 300},
]

4 个答案:

答案 0 :(得分:1)

您可以创建目标地址的哈希:

let data = [
  {
    macProtocol: 'ip',
    ipProtocol: 'tcp',
    dstAddress: 'X.60.219.13',
    tx: 50,
    rx: 100,
  },
  {
    macProtocol: 'ip',
    ipProtocol: 'tcp',
    dstAddress: 'X.60.219.13',
    tx: 50,
    rx: 100,
  },
  {
    macProtocol: 'ip',
    ipProtocol: 'tcp',
    dstAddress: 'X.60.219.13',
    tx: 50,
    rx: 100,
  },
  {
    macProtocol: 'ip',
    ipProtocol: 'tcp',
    dstAddress: 'X.60.219.13',
    tx: 50,
    rx: 100,
  },
  {
    macProtocol: 'ip',
    ipProtocol: 'udp',
    dstAddress: 'X.172.63.97',
    tx: 500,
    rx: 750,
  },
  {
    macProtocol: 'ip',
    ipProtocol: 'udp',
    dstAddress: 'X.189.157.90',
    tx: 1000,
    rx: 2776,
  },
  {
    macProtocol: 'ip',
    ipProtocol: 'udp',
    dstAddress: 'X.212.176.21',
    tx: 70,
    rx: 300,
  },
];
const reducedArr = Object.values(
  /*obj:  {
  'X.212.176.21': {
    macProtocol: 'ip',
    ipProtocol: 'udp',
    dstAddress: 'X.212.176.21',
    tx: 70,
    rx: 300,
  }, 
  'X...':{},
  .....
  }*/
  data.reduce((obj, {dstAddress:dAdd,...curr}) => {
    if (dAdd in obj) {
      ['tx', 'rx'].forEach(x => (obj[dAdd][x] += curr[x]));
    } else {
      obj[dAdd] = curr;
    }
    return obj;
  }, {})
);

console.log(reducedArr);
 

答案 1 :(得分:0)

您应该启动一个空数组,然后迭代抛出您的数据。看看吧:

const ips = [{"macProtocol":"ip","ipProtocol":"tcp","dstAddress":"X.60.219.13","tx":50,"rx":100}, {"macProtocol":"ip","ipProtocol":"tcp","dstAddress":"X.60.219.13","tx":50,"rx":100}, {"macProtocol":"ip","ipProtocol":"tcp","dstAddress":"X.60.219.13","tx":50,"rx":100}, {"macProtocol":"ip","ipProtocol":"tcp","dstAddress":"X.60.219.13","tx":50,"rx":100}, {"macProtocol":"ip","ipProtocol":"udp","dstAddress":"X.172.63.97","tx":500,"rx":750}, {"macProtocol":"ip","ipProtocol":"udp","dstAddress":"X.189.157.90","tx":1000,"rx":2776}, {"macProtocol":"ip","ipProtocol":"udp","dstAddress":"X.212.176.21","tx":70,"rx":300}];
const result = [];

ips.map(ip => {
  let currentIp = result.find(item => item.dstAddress === ip.dstAddress);
  
  if (currentIp) {
     currentIp.tx += ip.tx
     currentIp.rx += ip.rx
     result.push(currentIp)
  } else {
    result.push(ip)
  }
});

console.log(result);

答案 2 :(得分:0)

您可以使用JavaScript的reduce()方法来做到这一点。

var data = [
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.172.63.97", "tx":500, "rx":750},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.189.157.90", "tx":1000, "rx":2776},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.212.176.21", "tx":70, "rx":300},
]


let result = data.reduce((arr, currentValue) => {
   
   let index = arr.findIndex(item => item.dstAddress === currentValue.dstAddress);
   
   if (index === -1) {
      arr.push(currentValue);
   } else {
       arr[index].tx += currentValue.tx;
       arr[index].rx += currentValue.rx;
   }
  
   return arr;
  }, []);
      
 console.log(result);

答案 3 :(得分:0)

您可以:

  • 创建一个新数组
  • 循环阵列中的每一项,并检查其是否在新阵列中
  • 如果存在,则“求和” txrx值,否则将其压入新数组

let array = [
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "tcp", "dstAddress": "X.60.219.13", "tx":50, "rx":100},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.172.63.97", "tx":500, "rx":750},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.189.157.90", "tx":1000, "rx":2776},
  {"macProtocol": "ip", "ipProtocol": "udp", "dstAddress": "X.212.176.21", "tx":70, "rx":300},
];
let newArray = [];
array.forEach(item => {
  let itemExists = false;
  newArray.forEach(newItem => {
    if (newItem.dstAddress == item.dstAddress) {
      itemExists = true;
      newItem.tx += item.tx;
      newItem.rx += item.rx;
    }
  });
  if (!itemExists) {
    newArray.push(item);
  }
});
console.log(newArray);