Search code examples
javascriptnode.jsobjectcall

call value objects from other files into an object value


i have message object that contain :

const message = {
        headers: {
            i need to call another object to here
        }
};

and i have config object in another file :

exports.custom_headers{

    'x-my-key': 'header value',
    'x-another-key': 'another value'

}

i need to replace whole name and value of headers: with the object from config file

how to reference that ? I tried calling objects directly but it didn't work

normaly the object work like this

const message = {
    from: random_message.fromname+'<'+random_message.fromemail+'>',
    to: email,
    subject: random_message.subject,
    text: random_message.text,
    html: random_message.html,
    headers: {
            'x-my-key': 'header value',
            'x-another-key': 'another value'
    }
};

but i need to export headers to another file


Solution

  • If this is what you want. You can use ... (spread operator) to spread the keys of the object.

    const custom_headers =  {
        'x-my-key': 'header value',
        'x-another-key': 'another value'
    };
    
    const message = {
       from: 'random_message.fromname'+'<'+'random_message.fromemail'+'>',
       to: 'email',
       subject: 'random_message.subject',
       text: 'random_message.text',
       html: 'random_message.html',
      headers:{
        ...custom_headers
      }
    };
    
    console.log(message);