0

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'
 }
}
j0ck
  • 5
  • 2

1 Answers1

1

You can just pass the nested objects as parameters to the test function:

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
menestrello
  • 181
  • 1
  • 7
  • 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