Javascript 如何连接Node.js中未定义的变量?
我有大量的变量,如下所示,其中一些可能没有定义。什么是跳过所有未定义项的干净方法Javascript 如何连接Node.js中未定义的变量?,javascript,node.js,Javascript,Node.js,我有大量的变量,如下所示,其中一些可能没有定义。什么是跳过所有未定义项的干净方法 var sig = pin + order.id + order.operation_number + order.operation_type + order.operation_status + order.operation_amount + order.operation_currency + order.operation_withdrawal_amount + order.operation_co
var sig = pin + order.id + order.operation_number +
order.operation_type + order.operation_status + order.operation_amount
+ order.operation_currency + order.operation_withdrawal_amount +
order.operation_commission_amount + order.is_completed +
order.operation_original_amount + order.operation_original_currency +
order.operation_datetime + order.operation_related_number +
order.control + order.description + order.email + order.p_info +
order.p_email + order.credit_card_issuer_identification_number +
order.credit_card_masked_number + order.credit_card_expiration_year +
order.credit_card_expiration_month + order.credit_card_brand_codename
+ order.credit_card_brand_code + order.credit_card_id + order.channel
+ order.channel_country + order.geoip_country;
如果可以将所有这些值连接到一个数组中,只需使用
join()
将其余的值组合在一起即可。比如:
let order={
信用卡品牌代号:“代号”,
信用卡品牌代码:“品牌代码”,
信用卡id:“卡id”,
频道:未定义
}
//您要加入的名称
设值=[
订单、信用卡、品牌、代号、,
订单、信用卡、品牌代码、,
订单号、信用卡号、,
order.channel,
订单,一些未知的东西
]
设s=values.join(“”)//或.join(“”)来简单地连接
控制台日志
过滤掉你不需要的,然后把它连接起来。这是一种坏方法,在你发现字符串中的值未定义后。您可以将其替换为空。(笑) 如果需要,您可以替换
sig = sig.replace(/undefined/g, '');
console.log(sig); // I123100
为什么我提到这是一种糟糕的方式?
因为如果数据包含未定义的字符串,它将从数据中删除该字符串。这将丢失数据完整性
这是一种更好的方法不使用按循环键列出顺序
的所有属性。这很简单
var sig = pin;
for (key in order) sig += order[key] || '';
console.log(sig);
如果您想确定属性应该按顺序进行连接
,您可以使用映射
和连接
const keys = [
'id', 'operation_number', 'operation_type'
// any attribute you want...
];
var sig = pin + keys.map(i => order[i] || '').join('')
console.log(sig);
希望它能帮助您:)可能是
值。join(“”)
是更好的解决方案,但是如果您想更喜欢,您可以使用标记的模板文本来过滤出错误的值并返回一个干净的字符串
为了证明有这样一件事,这里有一个例子:
函数过滤器(字符串,…值){
让res='';
strings.forEach((string,i)=>{
if(值[i]){
res+=字符串+值[i];
}
});
返回res;
}
const test=filter`${undefined}${'something'}${123}${undefined}`;
控制台日志(测试)
这是一个简单的解决方案,只需使用Object.keys
获取键,并使用reduce
连接结果即可
// You order object
const order = {
id: 1050 ,
operation_status: undefined,
operation_type: 'unknown',
operation_amount: undefined
};
// Get the keys and then reduce to combine the results
const sig = Object.keys(order).reduce((all, item) => {
if (order[item]) {
all = `${all} ${order[item]}`;
}
return all;
}, '');
// Log
console.log(sig);
并不总是有效:
['i-contain-undefined',undefined].join('').replace(/undefined/g'')
将计算为'i-contain-'
@NinoFiliu,我提到过这是一种不好的方法,我还有其他方法来解决问题。Tnx
const keys = [
'id', 'operation_number', 'operation_type'
// any attribute you want...
];
var sig = pin + keys.map(i => order[i] || '').join('')
console.log(sig);
// You order object
const order = {
id: 1050 ,
operation_status: undefined,
operation_type: 'unknown',
operation_amount: undefined
};
// Get the keys and then reduce to combine the results
const sig = Object.keys(order).reduce((all, item) => {
if (order[item]) {
all = `${all} ${order[item]}`;
}
return all;
}, '');
// Log
console.log(sig);