0

I am trying to combine the values of a JSON object to display full throughput for traffic destined for a specific target, instead of tiny values to the same address.

I have read through hundreds of threads and they all seem to revolve around adding values from different arrays into one using an individual key, or by combining the values into a single field.

The key here is the IP address, the values to add are RX and TX

The data I have looks like this:

[
  {"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},
]

What I would like to turn this into:

[
  {"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},
]
6
  • There's no such thing as a "JSON Object" Commented Sep 8, 2019 at 16:37
  • Thanks. JSON array. Commented Sep 8, 2019 at 16:40
  • There's also no such thing as a "JSON array". What you have there is an array of objects Commented Sep 8, 2019 at 16:41
  • That said... What have you tried so far to solve the problem on your own? Commented Sep 8, 2019 at 16:41
  • I searched the internet for JSON Objects after you posted your initial comment and came across this: w3schools.com/js/js_json_objects.asp I have tried this piece of code found on this site with a few variations, however it just adds the values, and does not combine them. I will update with the code. Commented Sep 8, 2019 at 17:02

4 Answers 4

1

You could create a hash of destination address:

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this worked perfectly. Not sure how to mark it as accepted or answered, so assistance would be appreciated.
@Matthew You can accept it by clicking the check button on the left of my answer post
0

You should initiate an empty array, and iterate throw your data. Take a look on it:

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);

1 Comment

That's not how Array.prototype.map() should be used.
0

You can use JavaScript's reduce() method to do that.

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);

Comments

0

You could:

  • create a new array
  • cycle every item of your array and check if it exists on the new array
  • if it exists then "sum" tx and rx values, else push it on the new array

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);

2 Comments

Thanks! However, when I click "Run code Snippet" it has removed all the duplicates, but it has not combined the values of the x.60.219.13 rows?
@Matthew: I'm sorry, I didn't notice that I had to "sum" the values of rx and tx. Now I updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.