Jeff Mai Jeff Mai - 28 days ago 7
Javascript Question

Transforming an array in javascript

I am new to javascript and am struggling to find an answer to this.

I have an object with a list of network interfaces and details. I guess this is an array of objects?:

> console.log(net);
[ { Interface: 'wlan0:',
bytes: { Receive: '26087318', Transmit: '8475' },
packets: { Receive: '379665', Transmit: '52' },
errs: { Receive: '0', Transmit: '0' },
drop: { Receive: '0', Transmit: '0' },
fifo: { Receive: '0', Transmit: '0' },
frame: { Receive: '0' },
compressed: { Receive: '0', Transmit: '0' },
multicast: { Receive: '0' },
colls: { Transmit: '0' },
carrier: { Transmit: '0' } },
{ Interface: 'lo:',
bytes: { Receive: '228483', Transmit: '228483' },
packets: { Receive: '4046', Transmit: '4046' },
errs: { Receive: '0', Transmit: '0' },
drop: { Receive: '0', Transmit: '0' },
fifo: { Receive: '0', Transmit: '0' },
frame: { Receive: '0' },
compressed: { Receive: '0', Transmit: '0' },
multicast: { Receive: '0' },
colls: { Transmit: '0' },
carrier: { Transmit: '0' } },
{ Interface: 'mon0:',
bytes: { Receive: '285735889', Transmit: '0' },
packets: { Receive: '1753373', Transmit: '0' },
errs: { Receive: '0', Transmit: '0' },
drop: { Receive: '1765559', Transmit: '0' },
fifo: { Receive: '0', Transmit: '0' },
frame: { Receive: '0' },
compressed: { Receive: '0', Transmit: '0' },
multicast: { Receive: '0' },
colls: { Transmit: '0' },
carrier: { Transmit: '0' } },
{ Interface: 'eth0:',
bytes: { Receive: '6982603', Transmit: '22526425' },
packets: { Receive: '58906', Transmit: '384822' },
errs: { Receive: '0', Transmit: '0' },
drop: { Receive: '0', Transmit: '0' },
fifo: { Receive: '0', Transmit: '0' },
frame: { Receive: '0' },
compressed: { Receive: '0', Transmit: '0' },
multicast: { Receive: '0' },
colls: { Transmit: '0' },
carrier: { Transmit: '0' } } ]


I want to transform into this result:

> console.log(result);
{ lo: 'lo',
mon0: 'mon0',
eth0: 'eth0',
wlan0: 'wlan0' }


I have tried a few things in the node REPL console but I'm not getting anywhere:

> var result = {};
undefined
> Object.keys(net).forEach(function(ifname) {
... result[ifname] = ifname;
... });
undefined
> result
{ '0': '0', '1': '1', '2': '2', '3': '3' }


Another try:

> procfs.net(function(err,net){
... result = net.map(function(a) {return a.Interface;})
... });
undefined
> console.log(result);
[ 'wlan0:', 'lo:', 'mon0:', 'eth0:' ]
undefined


I can reference the data elements required individually, just struggling to put it all together:

> net[0].Interface
'wlan0:'
> net[1].Interface
'lo:'
> net[2].Interface
'mon0:'
> net[3].Interface
'eth0:'


Help appreciated!

Answer

EDIT: added the code to remove trailing colon character, to match the requested output format exactly

Just reduce it like that:

var result = net.reduce((ifaces,i) => { 
  var name = i.Interface.replace(/:$/,'');
  ifaces[name]=name; 
  return ifaces; 
},{})

Sample output

>>result
{ wlan0: "wlan0", lo: "lo", mon0: "mon0", eth0: "eth0" }