Javascript,关闭多个设备

时间:2018-10-16 15:48:18

标签: javascript arrays foreach

我重写了代码,现在我有了三盏灯,它们一一关掉。 有没有办法将它们全部放在一个变量中,以便只运行一次off命令?

// Turn off a bunch of lights

// Specify ID for a specific lamp
let KitchentableRight = await Homey.devices.getDevice({id: 'ef6bae9c-ec57-46ff-9011-905f90d16589'});
let KitchentableMiddle = await Homey.devices.getDevice({id: 'e29ab963-5139-4fdb-b4ec-ec25c103a62a'});
let KitchentableLeft = await Homey.devices.getDevice({id: '8b102bc6-00dd-4305-b111-f67a53ffd9d9'});

// Turn off tha lights based on their ID:s
KitchentableRight.setCapabilityValue('onoff', false);
KitchentableMiddle.setCapabilityValue('onoff', false);
KitchentableLeft.setCapabilityValue('onoff', false);

return true

1 个答案:

答案 0 :(得分:-1)

要使循环在每个设备上运行,应为:

 devices.forEach(device => {
       if(device.name) return false;
        console.log(device.name);
        device.setCapabilityValue('onoff', !device.state.onoff);
    })

如果a是设备名称的数组,则可以通过以下方式检查设备中的设备:a.indexOf(device.name) != -1