Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Invert boolean Values in JS object array

I’m dealing with object array

var data = [
    {
        "rdd": "Transducer Failure",
        "performance": true,
        "agc": true,
        "snr": true,
        "sos": true,
        "flowvel": true
    },
    {
        "rdd": "Detection Problem",
        "performance": false,
        "agc": false,
        "snr": false,
        "sos": false,
        "flowvel": false
    },
    {
        "rdd": "Ultrasonic Noise",
        "performance": false,
        "agc": false,
        "snr": false,
        "sos": false,
        "flowvel": false
    },
    {
        "rdd": "Process Condition Pressure",
        "performance": false,
        "agc": false,
        "snr": false,
        "sos": false,
        "flowvel": false
    },
    {
        "rdd": "Process Condition Temperature",
        "performance": false,
        "agc": true,
        "snr": false,
        "sos": true,
        "flowvel": false
    },
    {
        "rdd": "Fouling",
        "performance": false,
        "agc": false,
        "snr": false,
        "sos": false,
        "flowvel": false
    },
    {
        "rdd": "Changes in flow profile",
        "performance": false,
        "agc": false,
        "snr": false,
        "sos": false,
        "flowvel": false
    },
    {
        "rdd": "High Velocity",
        "performance": true,
        "agc": true,
        "snr": true,
        "sos": false,
        "flowvel": false
    }
]

Now I want to invert value of object, whichever is false make true and vice verse. also, need to extract key’s whose value is True after inversion .. I tried couple of things but no luck.
any idea ??

EDIT :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I Tried using

console.log(data);

for (var key in data) {
    var obj = data[key];
    Object.entries(obj).forEach(([key, value]) => {  
      if(value == false){
        value = true;
      }
        })
}

console.log(data)

result remains same

>Solution :

You could check the type of value and get the negated value or value of not boolean.

const
    data = [{ rdd: "Transducer Failure", performance: true, agc: true, snr: true, sos: true, flowvel: true }, { rdd: "Detection Problem", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Ultrasonic Noise", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Process Condition Pressure", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Process Condition Temperature", performance: false, agc: true, snr: false, sos: true, flowvel: false }, { rdd: "Fouling", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Changes in flow profile", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "High Velocity", performance: true, agc: true, snr: true, sos: false, flowvel: false }],
    result = data.map(o => Object.fromEntries(Object
        .entries(o)
        .map(([k, v]) => [k, typeof v === 'boolean' ? !v : v])
    ));
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading