我是javascript的新手,我正在努力寻找答案。
我有一个包含网络接口和详细信息列表的对象。我想这是一个对象数组?:
> 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' } } ]
我想转换成这个结果:
> console.log(result);
{ lo: 'lo',
mon0: 'mon0',
eth0: 'eth0',
wlan0: 'wlan0' }
我在节点REPL控制台中尝试过一些东西,但我没有到达任何地方:
> var result = {};
undefined
> Object.keys(net).forEach(function(ifname) {
... result[ifname] = ifname;
... });
undefined
> result
{ '0': '0', '1': '1', '2': '2', '3': '3' }
另一次尝试:
> procfs.net(function(err,net){
... result = net.map(function(a) {return a.Interface;})
... });
undefined
> console.log(result);
[ 'wlan0:', 'lo:', 'mon0:', 'eth0:' ]
undefined
我可以单独引用所需的数据元素,只是努力将它们放在一起:
> net[0].Interface
'wlan0:'
> net[1].Interface
'lo:'
> net[2].Interface
'mon0:'
> net[3].Interface
'eth0:'
帮助表示赞赏!
答案 0 :(得分:1)
您需要从Interface
属性中获取值,并将此值分配给对象中的相同键。
var 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' } }],
result = {};
net.forEach(function (o) {
result[o.Interface] = o.Interface;
});
console.log(result);

答案 1 :(得分:1)
这是使用Array.reduce()的完美示例!
net.reduce((interfaces, interface) => {
interfaces[interface.Interface.slice(0, -1)] = interface.Interface.slice(0, -1)
return interfaces
}, {})
const 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' } }]
const result = net.reduce((interfaces, interface) => {
interfaces[interface.Interface.slice(0, -1)] = interface.Interface.slice(0, -1)
return interfaces
}, {})
console.log(result)
答案 2 :(得分:1)
编辑:添加了删除尾部冒号字符的代码,以准确匹配请求的输出格式
只需减少就像那样:
var result = net.reduce((ifaces,i) => {
var name = i.Interface.replace(/:$/,'');
ifaces[name]=name;
return ifaces;
},{})
示例输出
>>result
{ wlan0: "wlan0", lo: "lo", mon0: "mon0", eth0: "eth0" }
或者,您可以使用Lodash(https://lodash.com/):
_.keyBy(net.map(i=>i.Interface.slice(0,-1)))
或其他一些实用程序库。