Home > database >  How do I insert a new object into object and unwrap this new object?
How do I insert a new object into object and unwrap this new object?

Time:10-27

I have an object like this

datt.js file:

const datt = {
  sub: {
    first: 'Jhon',
    last: 'Doe',
  }
}

dott.js file:

const dott = {
  company: 'wfh',
  fax: '0000',
};

module.exports = dott;

so I'm gonna import this dott file to datt file, what I expected is like this

console.log(datt)
/*
   sub: {
    first: 'Jhon',
    last: 'Doe',
    company: 'wfh',
    fax: '0000',
   }
*/

and I use this code

const dott = require('./dott.js');
const datt = {
  sub: {
    first: 'Jhon',
    last: 'Doe',
    dott,
  }
}

but I got like this:

/*
   sub: {
    first: 'Jhon',
    last: 'Doe',
    dott: {
      company: 'wfh',
      fax: '0000',
    }
   }
*/

this is just a small example, and the data what I want to import is more than 1 file. Thank you.

CodePudding user response:

...
first: 'Jhon',
last: 'Doe',
dott,
...

This syntax shortcut means to append a new property named dott with value dott (the object), which is a dictionary.

You should use the spread operator

{ ...dott }
  • Related