【问题标题】:How to find the max occurrence (highest count) of a value in an Array of Objects in JSON format?如何在 JSON 格式的对象数组中查找值的最大出现次数(最高计数)?
【发布时间】:2021-04-25 00:19:08
【问题描述】:

我有以下格式的数据:

 [
        {
            "product": "Term Insurance",
            "ViewTime": "Wed Jan 20 14:13:05 IST 2021",
            "userBought": true,
            "userLocation": {
                "loaded": true,
                "userAllowed": true,
                "coordinates": {
                    "lng": 70.8856,
                    "lat": 19.0748
                }
            },
            "category": "Health Insurance"
        },
        {
            "product": "Car Insurance",
            "ViewTime": "Wed Jan 20 15:40:31 IST 2021",
            "userBought": true,
            "userLocation": {
                "loaded": true,
                "userAllowed": true,
                "coordinates": {
                    "lng": 70.8856,
                    "lat": 19.0748
                }
            },
            "category": "Motor Insurance"
        },
        {
            "product": "Car Insurance",
            "ViewTime": "Wed Jan 20 15:42:52 IST 2021",
            "userBought": true,
            "userLocation": {
                "loaded": true,
                "userAllowed": true,
                "coordinates": {
                    "lng": 70.8856,
                    "lat": 19.0748
                }
            },
            "category": "Motor Insurance"
        }]

我想通过数组映射并使用 Javascript 从出现次数中计算 maxCategory 和 maxProduct。就像这里的 maxCategory 将是“汽车保险”,而 maxProduct 将是“汽车保险”。有什么干净、有效的方法吗?

【问题讨论】:

  • 你试过了吗?什么不起作用?请添加您的代码。
  • 请说明您所做的研究以及基于该研究所做的任何尝试。

标签: javascript arrays json reactjs loops


【解决方案1】:

你可以计算属性然后应用reduce方法,试试这个:

let cars = [
        {
            "product": "Term Insurance",
            "ViewTime": "Wed Jan 20 14:13:05 IST 2021",
            "userBought": true,
            "userLocation": {
                "loaded": true,
                "userAllowed": true,
                "coordinates": {
                    "lng": 70.8856,
                    "lat": 19.0748
                }
            },
            "category": "Health Insurance"
        },
        {
            "product": "Car Insurance",
            "ViewTime": "Wed Jan 20 15:40:31 IST 2021",
            "userBought": true,
            "userLocation": {
                "loaded": true,
                "userAllowed": true,
                "coordinates": {
                    "lng": 70.8856,
                    "lat": 19.0748
                }
            },
            "category": "Motor Insurance"
        },
        {
            "product": "Car Insurance",
            "ViewTime": "Wed Jan 20 15:42:52 IST 2021",
            "userBought": true,
            "userLocation": {
                "loaded": true,
                "userAllowed": true,
                "coordinates": {
                    "lng": 70.8856,
                    "lat": 19.0748
                }
            },
            "category": "Motor Insurance"
        }]

let counterCategories = {}
for (property of cars){
counterCategories[property.category] = 1 + (counterCategories[property.category] || 0)
}
console.log(counterCategories)


let entries = Object.entries(counterCategories);
let result = entries.reduce((acc, el) => acc[1] < el[1] ? el[0] : acc[0])
console.log('maxCategory: ' + result)

【讨论】:

    【解决方案2】:

    因为您正在寻找一种有效的方法来做到这一点。我使用了一个循环来找出结果。你可以检查一下。

    const data = [
        {
            product: 'Term Insurance',
            ViewTime: 'Wed Jan 20 14:13:05 IST 2021',
            userBought: true,
            userLocation: {
                loaded: true,
                userAllowed: true,
                coordinates: {
                    lng: 70.8856,
                    lat: 19.0748,
                },
            },
            category: 'Health Insurance',
        },
        {
            product: 'Car Insurance',
            ViewTime: 'Wed Jan 20 15:40:31 IST 2021',
            userBought: true,
            userLocation: {
                loaded: true,
                userAllowed: true,
                coordinates: {
                    lng: 70.8856,
                    lat: 19.0748,
                },
            },
            category: 'Motor Insurance',
        },
        {
            product: 'Car Insurance',
            ViewTime: 'Wed Jan 20 15:42:52 IST 2021',
            userBought: true,
            userLocation: {
                loaded: true,
                userAllowed: true,
                coordinates: {
                    lng: 70.8856,
                    lat: 19.0748,
                },
            },
            category: 'Motor Insurance',
        },
    ];
    
    let [maxProductCount, maxCategoryCount, maxProduct, maxCategory, tmpObj] = [0, 0, null, null, {}];
    
    data.forEach(({ product, category }) => {
        tmpObj[product] = (tmpObj[product] || 0) + 1;
        tmpObj[category] = (tmpObj[category] || 0) + 1;
        if (tmpObj[product] > maxProductCount) {
            maxProductCount = tmpObj[product];
            maxProduct = product;
        }
        if (tmpObj[category] > maxCategoryCount) {
            maxCategoryCount = tmpObj[category];
            maxCategory = category;
        }
    });
    
    console.log({ maxProduct, maxCategory });

    【讨论】:

      【解决方案3】:

      看看lodashcountBy方法:

      let data = [...]
      _.countBy(data, (item) => item.category);
      // {"Health Insurance": 1, "Motor Insurance": 2}
      _.countBy(data, (item) => item.product);
      // {"Term Insurance": 1, "Car Insurance": 2}
      

      【讨论】:

        猜你喜欢
        • 2013-06-22
        • 2011-04-29
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 2015-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多