const obj = {
foo: 1,
bar: 2
};
let fn = (...obj) => {
console.log(foo, bar); // ReferenceError: foo is not defined
};
fn(obj);
I basically want to use the object props directly inside the function without having to obj.foo
or obj.bar
.
Edited to add: I understand I can define the args manually ({ foo, bar }
), but I don't want to do that since the object is expected to have unknown / variable number of properties.
Does this help?
I assume you are after some properties that you do know about. But want to retain the others ?
const obj = {
foo: 1,
bar: 2,
unknown: 3,
unknow: 4,
};
let fn = ({ foo, bar, ...other}) => {
console.log(foo, bar);
console.log(other);
};
fn(obj);