0

I have the following array of object. I want to get total count where properties are true. i.e in this case total enabled is 5

let data = 
[
    {
        comment: true,
        attachment: true,
        actionPlan: true
    },
    {
        whenValue: '',
        comment: true,
        attachment: false,
        actionPlan: true
    }
]

I tried something below but it didnt worked.

const countObj = questionAttributes.questionMandatoryOptions.reduce((acc, curr) => {
    return {
        //want to get properties having true values
    };
});

For single item in array i can achieve with followign:

const total =
            options[0];
        const totalelections = Object.values(
            total
        ).filter((v) => v).length;
Alejandro
  • 7,290
  • 4
  • 34
  • 59
Mukil Deepthi
  • 6,072
  • 13
  • 71
  • 156
  • `I tried something below but it didnt work.` I can hardly call an empty function an attempt at solving the problem... I wish putting my expected output in a comment would get me the expected output, too! –  Jul 18 '22 at 12:59

2 Answers2

0

Will work for the following ts configs: "target": "es2019", or if lib contains "esnext".

Details: flatMap, flat, flatten doesn't exist on type any[]

{
  "compilerOptions": {
    "target": "es2015",
    "lib": [
      "dom",
      "esnext"
    ]
  }
}
let data = [{
    comment: true,
    attachment: true,
    actionPlan: true
  },
  {
    whenValue: '',
    comment: true,
    attachment: false,
    actionPlan: true
  }
];

const countObj = data
  .map(x => Object.values(x)) // [[true, true, true], ["", true, false, true]]
  .flatMap(x => x) // [true, true, true, "", true, false, true]
  .filter(x => x === true) // [true, true, true, true, true]
  .length; // 5

console.log(countObj); // 5

Edit TypeScript (forked)

Sergey Sosunov
  • 4,124
  • 2
  • 11
  • 15
  • Getting flatMap does not exist on type any[][] – Mukil Deepthi Jul 18 '22 at 12:54
  • my tsconfig looks like "target": "es2015", "typeRoots": [ "node_modules/@types" ], "lib": [ "es2017", "dom" ] – Mukil Deepthi Jul 18 '22 at 13:00
  • Your example doesn't compile in a simple [playground](https://www.typescriptlang.org/play?#code/DYUwLgBAJghmMQLwQNoG8CwAoCuIGMB7AW2JADswAuCMAJwFcQAabPCOefACzMpvpNWOPDHxgAloXIAFYDHIDGINhAC+w3JhG4A7twoA1GMCY0A5Oc14ipCtVrLruTmN72aAMxMBnFqpdxKVl5RUcmVTVsAF0AbmxsInIfSCIGSgB5ACMAKyRoOBhVADpiGAAHAAoADyQAPghsnJBxYoA3EyYfGoBKHogAegHUFEEWcPGx6OZUACJZmbGZ72A-ReVo6JLPeTAAWQqa+ohq-qHUJYn1oQh56-GVtYmtkWLPCWAwEDojxAbaxCAiZnYajJxXCGXKYlUDkADmYG4sUGwwArAksEkfIRQMVgIQ4ZU0plcj1kedUUA) –  Jul 18 '22 at 13:00
  • questionAttributes.questionMandatoryOptions is an object array – Mukil Deepthi Jul 18 '22 at 13:01
  • Unfortunately i can't switch to 'es2019'. can u pls provide some other solution? Thanks – Mukil Deepthi Jul 18 '22 at 13:08
0

You alresdy found the solution for a single element, so all you really need to do is to iterate over every element in your data and increase a counter with the result of

Object.values(item).filter((v) => v).length;

let data = 
[
    {
        comment: true,
        attachment: true,
        actionPlan: true
    },
    {
        whenValue: '',
        comment: true,
        attachment: false,
        actionPlan: true
    }
]

function countTrueProperties(a_data: any[]): number {
    let count = 0;
    for(const item of a_data){
        count += Object.values(item).filter((v) => v).length;
    }
    return count;
}

console.log(countTrueProperties(data));

Demo playground