将其他文件中的值 objects 调用为 object 值
call value objects from other files into an object value
我的消息 object 包含:
const message = {
headers: {
i need to call another object to here
}
};
我在另一个文件中有配置 object :
exports.custom_headers{
'x-my-key': 'header value',
'x-another-key': 'another value'
}
我需要用配置文件
中的 object 替换 headers 的整个名称和值
如何引用?
我试过直接调用 objects 但没有成功
通常 object 是这样工作的
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'
}
};
但我需要将 headers 导出到另一个文件
如果这是你想要的。您可以使用 ...
(扩展运算符)来扩展对象的键。
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);
//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]; });
我的消息 object 包含:
const message = {
headers: {
i need to call another object to here
}
};
我在另一个文件中有配置 object :
exports.custom_headers{
'x-my-key': 'header value',
'x-another-key': 'another value'
}
我需要用配置文件
中的 object 替换 headers 的整个名称和值如何引用? 我试过直接调用 objects 但没有成功
通常 object 是这样工作的
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'
}
};
但我需要将 headers 导出到另一个文件
如果这是你想要的。您可以使用 ...
(扩展运算符)来扩展对象的键。
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);
//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]; });