简体   繁体   中英

How to get object value which has a space and a special character in its key and object name with node js

Below I have a value in one variable called publicIdentifier . Using console.log("publicIdentifier",publicIdentifier); I am getting the value below. I have to get the value of MSISDN-MSISDN into differnet variable. I am getting below request which value is in sting so I am making in JSON and then I am trying to get this MSISDN-MSISDN. Is there any way that I can take it withought making JSON that is also fine. Please help.

 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"

I tried the code below, but it's not working in node 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);
        }
    }

It should just work, even without parsing. Perhaps there's some typo in your code?

edit : given your edited question I suppose the error is here

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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM