Home > database >  Count number of occurrences of JSON property with name like
Count number of occurrences of JSON property with name like

Time:11-16

I have a Vue app that gets a JSON object like below. I need to count the number of items where _Valid is true. I have very little experience with the Reduce method in Javascript and don't know how to filter on a portion of a key name. Where do I begin? Is this possible?

{
    "F_Mountain1_Week": "1",
    "F_Mountain1_Date": "2021-11-12",
    "F_Mountain1_Valid": true,
    "F_Mountain2_Week": "1",
    "F_Mountain2_Date": "2021-11-12",
    "F_Mountain2_Valid": true,
    "F_Mountain3_Week": "1",
    "F_Mountain3_Date": "2021-11-12",
    "F_Mountain3_Valid": true,
    "F_Mountain4_Week": "",
    "F_Mountain4_Date": "2021-11-12",
    "F_Mountain4_Valid": false,
    "F_Mountain5_Week": "",
    "F_Mountain5_Date": "2021-11-12",
    "F_Mountain5_Valid": false,
    "F_Mountain6_Week": "",
    "F_Mountain6_Date": "2021-11-12",
    "F_Mountain6_Valid": false,
    "F_Mountain7_Week": "",
    "F_Mountain7_Date": "2021-11-12",
    "F_Mountain7_Valid": false,
    "F_Mountain8_Week": "",
    "F_Mountain8_Date": "2021-11-12",
    "F_Mountain8_Valid": false,
}

CodePudding user response:

This would also work

Using entries and filter. First, get the key and value pairs and then filter using the condition based on value and key properties.

let data = {
    F_Mountain1_Week: "1",
    F_Mountain1_Date: "2021-11-12",
    F_Mountain1_Valid: true,
    F_Mountain2_Week: "1",
    F_Mountain2_Date: "2021-11-12",
    F_Mountain2_Valid: true,
    F_Mountain3_Week: "1",
    F_Mountain3_Date: "2021-11-12",
    F_Mountain3_Valid: true,
    F_Mountain4_Week: "",
    F_Mountain4_Date: "2021-11-12",
    F_Mountain4_Valid: false,
    F_Mountain5_Week: "",
    F_Mountain5_Date: "2021-11-12",
    F_Mountain5_Valid: false,
    F_Mountain6_Week: "",
    F_Mountain6_Date: "2021-11-12",
    F_Mountain6_Valid: false,
    F_Mountain7_Week: "",
    F_Mountain7_Date: "2021-11-12",
    F_Mountain7_Valid: false,
    F_Mountain8_Week: "",
    F_Mountain8_Date: "2021-11-12",
    F_Mountain8_Valid: false
};

console.log(
    Object.entries(data).filter(([key, value]) =>  key.endsWith("_Valid") && value)
        .length
);

CodePudding user response:

You can just loop over the array using reduce and calculate the number of properties that endsWith _Valid and which is true as:

Object.keys(obj).reduce((acc, curr) => curr.endsWith("_Valid") && obj[curr] ? acc   1 : acc, 0)

or you can also use filter here as:

Object.keys(obj).filter((k) => k.endsWith("_Valid") && obj[k]).length;

const obj = {
  F_Mountain1_Week: "1",
  F_Mountain1_Date: "2021-11-12",
  F_Mountain1_Valid: true,
  F_Mountain2_Week: "1",
  F_Mountain2_Date: "2021-11-12",
  F_Mountain2_Valid: true,
  F_Mountain3_Week: "1",
  F_Mountain3_Date: "2021-11-12",
  F_Mountain3_Valid: true,
  F_Mountain4_Week: "",
  F_Mountain4_Date: "2021-11-12",
  F_Mountain4_Valid: false,
  F_Mountain5_Week: "",
  F_Mountain5_Date: "2021-11-12",
  F_Mountain5_Valid: false,
  F_Mountain6_Week: "",
  F_Mountain6_Date: "2021-11-12",
  F_Mountain6_Valid: false,
  F_Mountain7_Week: "",
  F_Mountain7_Date: "2021-11-12",
  F_Mountain7_Valid: false,
  F_Mountain8_Week: "",
  F_Mountain8_Date: "2021-11-12",
  F_Mountain8_Valid: false,
};

const result = Object.keys(obj).reduce((acc, curr) => curr.endsWith("_Valid") && obj[curr] ? acc   1 : acc, 0);

console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Old school style

let data = {
    F_Mountain1_Week: "1",
    F_Mountain1_Date: "2021-11-12",
    F_Mountain1_Valid: true,
    F_Mountain2_Week: "1",
    F_Mountain2_Date: "2021-11-12",
    F_Mountain2_Valid: true,
    F_Mountain3_Week: "1",
    F_Mountain3_Date: "2021-11-12",
    F_Mountain3_Valid: true,
    F_Mountain4_Week: "",
    F_Mountain4_Date: "2021-11-12",
    F_Mountain4_Valid: false,
    F_Mountain5_Week: "",
    F_Mountain5_Date: "2021-11-12",
    F_Mountain5_Valid: false,
    F_Mountain6_Week: "",
    F_Mountain6_Date: "2021-11-12",
    F_Mountain6_Valid: false,
    F_Mountain7_Week: "",
    F_Mountain7_Date: "2021-11-12",
    F_Mountain7_Valid: false,
    F_Mountain8_Week: "",
    F_Mountain8_Date: "2021-11-12",
    F_Mountain8_Valid: false
};

for (var prop in data) {
    if (typeof data[prop] == 'function') continue;
    if(/_Valid$/.test(prop) && data[prop] === true) {
        console.log('BAZINGA!')
    } else {
        console.log('I DON\'T CARE ABOUT THIS PROP:\n')
        console.log(`Property name: ${prop} - Value: ${data[prop]}`);
    }
}
  • Related