You can describe an object in this way
const [a, b] = Object.values(obj);
console.log(a);
console.log(b);
Remember that the keys of an object are not alphabetic, so it might be better to create a function that returns sorted keys so that you know that the values are set correctly.
function deconstructObject(obj){
const arr = [];
const keys = Object.keys(obj);
const sortedKeys = keys.sort();
for(const key of sortedKeys){
arr.push(obj[key]);
}
return arr;
}
const [a, b] = deconstructObject({b: 2, a: 1 });
console.log(a);
console.log(b);
const newArray = deconstructObject({b: 2, a: 1 });
console.log(newArray);
Now the object will be sorted, so you can predict its behavior.
Pavlo source
share