I have an object and array, every element in array corresponds to object property.
It looks line this:
let keys = { kShift: null, kSpace: null, kEnter: null, kA: null, kS: null, kD: null, kW: null } let array = document.querySelectorAll('.k')
<div class="k">shift</div> <div class="k">space</div> <div class="k">enter</div> <div class="k">a</div> <div class="k">s</div> <div class="k">d</div> <div class="k">w</div>
I need every property of object to get corresponding value from array.
it looks so:
keys.kShift = array[0] // first property of object is the first element in array
keys.kSpace = array[1] // second property of object is the second element in array
keys.kEnter = array[2] // third property of object is the third element in array
and so on
So i need to find the best way to do it
Using ES6 destructuring for array. Have to make sure the array items on right are in same order as the declared variables.
const [ kShift, kSpace, kEnter, kA, kS, kD, kW ] = document.querySelectorAll('.k')
const keys = { kShift, kSpace, kEnter, kA, kS, kD, kW }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.