js中有一个数组enableFeaturesArr一个enableFeatures:enableFeatures deviceParam false meterData false valveCtrl false csqRead false 现在将enableFeatures中key在enab
你可以使用Object.keys()方法来获取enableFeatures对象的所有键,然后使用forEach循环遍历这些键。在循环中,你可以检查当前键是否在enableFeaturesArr数组中,如果是则将对应的值设置为true,否则设置为false。
以下是具体的代码实现:
const enableFeatures = {
deviceParam: false,
meterData: false,
valveCtrl: false,
csqRead: false
};
const enableFeaturesArr = ['deviceParam', 'valveCtrl'];
Object.keys(enableFeatures).forEach(key => {
enableFeatures[key] = enableFeaturesArr.includes(key);
});
console.log(enableFeatures);
输出结果为:
{
deviceParam: true,
meterData: false,
valveCtrl: true,
csqRead: false
}
在这个例子中,enableFeaturesArr数组中包含了deviceParam和valveCtrl两个键。因此,enableFeatures对象中对应的值被设置为true,其他键的值则被设置为false
原文地址: https://www.cveoy.top/t/topic/iLTf 著作权归作者所有。请勿转载和采集!