вызывать объекты значения из других файлов в значение объекта - PullRequest
0 голосов
/ 16 сентября 2018

У меня есть объект сообщения, который содержит:

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

и у меня есть объект конфигурации в другом файле:

exports.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: {
            'x-my-key': 'header value',
            'x-another-key': 'another value'
    }
};

но мне нужно экспортировать заголовки в другой файл

Ответы [ 2 ]

0 голосов
/ 16 сентября 2018
//basically you have 2 object and wants to override one object's with another

//here is one object
const message = {
        headers: {
            "old-header1": "old-header-value1",
        "common-property": "old-header-value2"
        }
};

//here is config object
const config = {
    "new-header1": "new-header-value1",

    "common-property": "new-header-value2"
};

//message.headers = {}; //uncomment this if you want to remove all older properties

//to override properties
Object.keys(config).forEach(function(d) { message.headers[d] = config[d]; });
0 голосов
/ 16 сентября 2018

Если это то, что вы хотите. Вы можете использовать ... (оператор распространения) для распространения ключей объекта.

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);
...