In javascript even if you created your object using a number
as a key, the keys are always strings, take the following example:
const obj = {
[1]: 1,
anotherThing: 2
};
console.log(Object.keys(obj))
However, since '1'
or 1
are literals, using them in the destructuring assignment is not possible, in order to use the numerical keys you would have to convert your object to iterable
or map properties manually,
The easiest way to convert your object to iterable is to use Object.assign
with an array, since an array is also an object you can do the following:
const arr = Object.assign([], obj);
And to fulfill your purpose, something like:
const obj = {
[0]: 0,
[1]: 1,
wheaterData: 'wheaterData',
anotherNonNumericProp: '1'
};
const arrOfNumericProps = Object.assign([], obj);
const [prop0, prop1] = arrOfNumericProps;
const {wheaterData,
...rest
} = arrOfNumericProps;
console.log(prop0);
console.log(prop1);
console.log(wheaterData);
console.log(rest);