访问数组中的JavaScript对象

时间:2018-08-04 15:07:13

标签: javascript arrays multidimensional-array

运行console.log(os.networkInterfaces());时,得到如下所示的输出。

如何在Javascript的以太网4下访问家族?

我尝试过:

console.log(os.networkInterfaces().Ethernet 4[0].family);

但是它不起作用。

console.log(os.networkInterfaces());的输出

{ 'Ethernet 4':
   [ { address: '9.0.0.1',
       netmask: '255.255.255.0',
       family: 'IPv4',
       mac: '00:ff:8c:22:df:9d',
       internal: false,
       cidr: '9.0.0.1/24' } ],
  'Wi-Fi':
   [ { address: 'fe80::2160:bed0:15d8:6a4c',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: 'f0:03:8c:9c:71:a1',
       scopeid: 10,
       internal: false,
       cidr: 'fe80::2160:bed0:15d8:6a4c/64' },
     { address: '192.168.0.3',
       netmask: '255.255.255.0',
       family: 'IPv4',
       mac: 'f0:03:8c:9c:71:a1',
       internal: false,
       cidr: '192.168.0.3/24' } ],
  'Loopback Pseudo-Interface 1':
   [ { address: '::1',
       netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
       family: 'IPv6',
       mac: '00:00:00:00:00:00',
       scopeid: 0,
       internal: true,
       cidr: '::1/128' },
     { address: '127.0.0.1',
       netmask: '255.0.0.0',
       family: 'IPv4',
       mac: '00:00:00:00:00:00',
       internal: true,
       cidr: '127.0.0.1/8' } ] }

3 个答案:

答案 0 :(得分:1)

您可以使用,

console.log(os.networkInterfaces()["Ethernet 4"][0].family);

演示

let myObject = {
  "Ethernet 4": [
    {
      "address": "9.0.0.1",
      "netmask": "255.255.255.0",
      "family": "IPv4",
      "mac": "00:ff:8c:22:df:9d",
      "internal": false,
      "cidr": "9.0.0.1/24"
    }
  ],
  "Wi-Fi": [
    {
      "address": "fe80::2160:bed0:15d8:6a4c",
      "netmask": "ffff:ffff:ffff:ffff::",
      "family": "IPv6",
      "mac": "f0:03:8c:9c:71:a1",
      "scopeid": 10,
      "internal": false,
      "cidr": "fe80::2160:bed0:15d8:6a4c/64"
    },
    {
      "address": "192.168.0.3",
      "netmask": "255.255.255.0",
      "family": "IPv4",
      "mac": "f0:03:8c:9c:71:a1",
      "internal": false,
      "cidr": "192.168.0.3/24"
    }
  ],
  "Loopback Pseudo-Interface 1": [
    {
      "address": "::1",
      "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
      "family": "IPv6",
      "mac": "00:00:00:00:00:00",
      "scopeid": 0,
      "internal": true,
      "cidr": "::1/128"
    },
    {
      "address": "127.0.0.1",
      "netmask": "255.0.0.0",
      "family": "IPv4",
      "mac": "00:00:00:00:00:00",
      "internal": true,
      "cidr": "127.0.0.1/8"
    }
  ]
};

console.log(myObject["Ethernet 4"][0].family);

答案 1 :(得分:1)

只需尝试

console.log(os.networkInterfaces()['Ethernet 4'][0].family)

答案 2 :(得分:0)

这样做:

console.log(os.networkInterfaces()['Ethernet 4'][0].family);