javascriptarraysjsobject

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 :

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