I just have a question if it is possible to destructure an object using an array of variables. I know it is possible if you know the name of the Object Keys but if you do not know the keys of the object (e.g. from an API) can this be done? Any suggestions would be great. Thank you.
const names = {
a: 'peter',
b: 'jane',
c: 'sam',
};
let vals = Object.keys(names);
console.log(vals);
// const { a, b, c } = names;
[vals] = names;
console.log(a, b, c);
2
Answers
Method 1:
Method 2:
If you want to get a value of list, there is a two way.
or