How to use a nested object as a function parameter? [duplicate]
How to use a nested object as a function parameter? [duplicate]
This question already has an answer here:
The following example is working:
const data1 =
first: 1,
second: 2
;
const data2 =
first: 'first',
second: 'second'
;
function test(obj)
console.log(obj.first, obj.second);
test(data1); // 1 2
test(data2); // first second
What do I have to do when I have data1 and data2 as nested objects in another object?
const data =
data1:
first: 1,
second: 2
,
data2:
first: 'first',
second: 'second'
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
test(data.data1)
:( It's so stressful when you spend several minutes writing a long good answer with examples and snippet and explanations, and suddenly the question is closed. Please, do not close question so quickly!
– Damián Pablo González
Sep 11 '18 at 15:17
Same here @DamiánPabloGonzález :|
– CodeDraken
Sep 11 '18 at 15:22
1 Answer
1
You can just pass the nested objects as parameters to the test
function:
test
const data =
data1:
first: 1,
second: 2
,
data2:
first: 'first',
second: 'second'
;
function test(obj)
console.log(obj.first, obj.second);
test(data.data1); // 1 2
test(data.data2); // first second
That's it. I was a little bit confused. I thought I had to modify the function parameter to get access to nested objects, e.g.
function test(obj.obj)
– j0ck
Sep 11 '18 at 15:19
function test(obj.obj)
test(data.data1)
? Is that what you're looking for?– Pointy
Sep 11 '18 at 14:59