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

How to find number of occurrences based on input value

I’m attempting to find the number of occurrences based on input value for ownerId in the JavaScript Json Object below. If the ownerId is the number "1" the result should be three since the ownerId appears 3 times.

ownerID: 3 

Here is what I have so far…

let inputValue = 1;
let return = "";

const result = jsonResponse.jsonArray[0].info.find(
(arr) => arr.ownderID === inputValue;  );
    
var data = jsonResponse.jsonArray[0].info
        .reduce((counts, result) => {
        const inputItem = result[inputValue];
        counts[inputItem] = (counts[inputItem] || 0) + 1;
        return counts;
      }, {
      });

    return = data;

JSON…

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

   const jsonResponse = {
   "jsonArray": [
     {
        "info": [
            {
                "ownerId": 1,
                "carType": "Nissan",
                "houseType": "Rancher"
            },
            {
                "ownerId": 1,
                "carType": "Ford",
                "houseType": "Trailer"
            }
        ],
        "familyInfo": {
            "kids": 1,
            "tuition": "yes",
            "parentId": 7
        }
    },
    {
        "info": [
            {
                "ownerId": 3,
                "carType": "Nissan",
                "houseType": "Single"
            }
        ],
        "familyInfo": {
            "kids": 4,
            "tuition": "no",
            "parentId": 11
        }
    },
    {
        "info": [
            {
                "ownerId": 1,
                "carType": "Chevy",
                "houseType": "TownHome"
            }
        ]
    }
  ]
}

Am I going about this the right way using find and reduce?

>Solution :

You are almost there, what you need to do is count across multiple jsonArrays and also within the infos

Look at the code below, I have commented it appropriately to make it verbose.
Hope it helps.

const jsonResponse = {
  "jsonArray": [{
      "info": [{
          "ownerId": 1,
          "carType": "Nissan",
          "houseType": "Rancher"
        },
        {
          "ownerId": 1,
          "carType": "Ford",
          "houseType": "Trailer"
        }
      ],
      "familyInfo": {
        "kids": 1,
        "tuition": "yes",
        "parentId": 7
      }
    },
    {
      "info": [{
        "ownerId": 3,
        "carType": "Nissan",
        "houseType": "Single"
      }],
      "familyInfo": {
        "kids": 4,
        "tuition": "no",
        "parentId": 11
      }
    },
    {
      "info": [{
        "ownerId": 1,
        "carType": "Chevy",
        "houseType": "TownHome"
      }]
    }
  ]
}

// what to search for?
const searchOwnerId = 1;

// reduce to count owner id across multiple items in the list
const counts = jsonResponse.jsonArray.reduce((acc, item) => {
  // count the owner ids inside the infos
  // filter for the ownerId and length works perfect
  
  acc += item.info.filter(
    ({ownerId}) => ownerId == searchOwnerId
  ).length

  return acc

// start reduce with a zero
}, 0);

// final output
console.log(counts)
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