You can manually set the values in a chain:(您可以手动设置链中的值:)
const obj = {}; obj.w = obj.a = obj.s = obj.d = false; console.log(obj)
You can iterate the array of keys with Array.forEach()
, and set the values:(您可以使用Array.forEach()
迭代键数组并设置值:)
const updateObj = (value, keys, obj) => keys.forEach(k => obj[k] = value) const obj = { w: true, a: true, s: true, d: true }; updateObj(false, Object.keys(obj), obj); console.log(obj) updateObj(true, ['w', 's'], obj); console.log(obj)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…