簡體   English   中英

如何使用節點 js 獲取 object 值,其鍵中包含空格和特殊字符以及 object 名稱

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM