[英]How to get object value which has a space and a special character in its key and object name with node js
下面我在一个名为publicIdentifier
的变量中有一个值。 使用console.log("publicIdentifier",publicIdentifier);
我得到下面的值。 我必须将MSISDN-MSISDN
的值转换为不同的变量。 我收到低于请求的值,所以我在 JSON 中制作,然后我试图获得这个 MSISDN-MSISDN。 有没有什么方法可以让我接受它而不需要制作 JSON 也很好。 请帮忙。
request:"{"Storm 1.5 GB":{"SIM Product-SIMType":"MobileSIM","MSISDN Product-MSISDN":"","MSISDN-MSISDN":"9900004681","SIM Product-SIMNumber":"8923401000003155"}}"
publicIdentifier=JSON.parse(request)
Storm 1.5 GB:Object {SIM Product-SIMType: "MobileSIM", MSISDN Product-MSISDN: "", MSISDN-MSISDN: "9900004672", …}
MSISDN Product-MSISDN:""
MSISDN-MSISDN:"9900004672"
SIM Product-SIMNumber:"8923401000003150"
SIM Product-SIMType:"MobileSIM"
我尝试了下面的代码,但它在节点 js 中不起作用。
publicIdentifier123=publicIdentifier['Storm 1.5 GB']['MSISDN-MSISDN']
loyaltyOnBoardMember = async (args, publicIdentifier) => {
//console.log("loyality args ",args.interactionItem[0].item);
const storm = publicIdentifier["Storm 1.5 GB"];
Object.keys(storm)
.forEach(key =>
console.log(`key: ${key}, value: ${publicIdentifier["Storm 1.5 GB"][key]}`));
const publicIdentifier2 = storm["Storm 1.5 GB"]["MSISDN-MSISDN"];
console.log("aaaaaaa", publicIdentifier2);
const value = args.interactionItem[0].item;
let uri = `${config.dlmUrl}/loyaltyManagement/loyaltyMember/loyaltyOnBoardMember`;
let contactMedium = [];
if (_.toLower(value.engagedParty.contactMedium[0].type) === 'mobile') {
contactMedium.push({
type: "Mobile",
medium: {
type: "mobile",
value: value.engagedParty.contactMedium[0].mediumargs.mobile
}
})
}
if (_.toLower(value.engagedParty.contactMedium[0].type) === 'emailaddress') {
contactMedium.push({
type: "EmailAddress",
medium: {
type: "EmailAddress",
value: value.engagedParty.contactMedium[0].medium.emailAddress
}
})
}
let request = {
publicIdentifier: publicIdentifier['Storm 1.5 GB MSISDN-MSISDN'],
name: value.engagedParty.givenName,
status: value.engagedParty.status,
validFor: {
startDateTime: value.createdDate
},
characteristic: [
{
name: "businessType",//characteristicName,
value: "Postpaid"//characteristicValue
}
],
engageParty: {
id: value.engagedParty.id,
contactMedium: contactMedium
}
};
logger.info(`${uri}`);
try {
logger.debug(request);
const response = await this.post(uri, request, this.options);
logger.debug(response);
return response;
} catch (error) {
this.exceptionHandler.handleException(error);
}
}
它应该可以正常工作,即使没有解析。 也许您的代码中有一些错字?
编辑:鉴于您编辑的问题,我想错误就在这里
let request = {
publicIdentifier: publicIdentifier['Storm 1.5 GB MSISDN-MSISDN'],
// ^ should be ...
// ... publicIdentifier['Storm 1.5 GB'']['MSISDN-MSISDN'],
const someObj = { "Storm 1.5 GB": { "SIM Product-SIMType":"MobileSIM", "MSISDN Product-MSISDN":"", "MSISDN-MSISDN":"9900004681", "SIM Product-SIMNumber":"8923401000003155" } }; // the first value const storm15Gb = someObj["Storm 1.5 GB"]; // check if values can be retrieved, using keys /w spaces/dashes Object.keys( storm15Gb ).forEach( key => console.log(`key: ${key}, value: ${someObj["Storm 1.5 GB"][key]}`) ); // to be sure const publicIdentifier = someObj["Storm 1.5 GB"]["MSISDN-MSISDN"]; console.log(publicIdentifier);
.as-console-wrapper { top: 0; max-height: 100%;important; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.