Search code examples
javascriptarraysjsonreduce

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...

   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)