Home > Net >  how to typecast raw string into object type in javascript
how to typecast raw string into object type in javascript

Time:01-20

I hope everyone is doing good. I'm beginner in Javascript. I'm working in Node.js with oracle database. I am having array of objects. Ex: If any of the keys have null value in Javascript. I want to replace dynamic text and then have to typecast a raw string into object type.

I couldn't typecast raw string into object in javascript.

I have attached the code.

Any help would be appreciated. Thanks in advance...

var myObject = [
    {
        r_bu: '146',
        cty_code: 'AFG',
        c_code: 'AFN',
        ps_code: '146',
        b_unit: null
    },
    {
        r_bu: '6',
        cty_code: 'AGO',
        c_code: 'AOA',
        ps_code: '6',
        b_unit: null
    },
    {
        r_bu: '138',
        cty_code: 'BHR',
        c_code: 'USD',
        ps_code: '138',
        b_unit: 'OBU'
    },
    {
        r_bu: '138',
        cty_code: 'BHR',
        c_code: 'USD',
        ps_code: '665',
        b_unit: 'OBU'
    }
];

console.log(myObject);

function hasNull(element, index, array) {
    return Object.keys(element).some(
        function (key) {
            if (element[key] === null) {
                element[key] = `${element[key]} OR ${key} IS NULL` // I have replaced text wanna typecast string to object eg: 'null OR b_unit IS NULL' to null OR b_unit IS NULL
            }
        }
    );
}

myObject.some(hasNull);

console.log(myObject);

My Expectation like this. enter image description here

CodePudding user response:

Your problem sounds like an XY-problem; however, if it really is as simple as wrapping a primitive string so that typeof reports it as an object then you can use:

let a = new String("IS NULL");
console.log(typeof a)

  • Related