【问题标题】:Count total with two criterias using Lodash使用 Lodash 使用两个条件计算总数
【发布时间】:2014-09-05 01:02:12
【问题描述】:

我不知道如何根据位置和唯一序列号计算设备总数。

{
"dates": [
    {
        "date": "Sep 1, 2014",
        "devices": [
            {
                "model": "Canon",
                "location": "Chicago",
                "serialNum": "abc123"
            },
            {
                "model": "Canon",
                "location": "Chicago",
                "serialNum": "xyz456"
            },
            {
                "model": "HP",
                "location": "New York",
                "serialNum": "123456"
            },
            {
                "model": "Brother",
                "location": "Chicago",
                "serialNum": "DEF777"
            }
        ]
    },
    {
        "date": "Sep 2, 2014",
        "devices": [
            {
                "model": "Canon",
                "location": "Chicago",
                "serialNum": "abc123"
            },
            {
                "model": "Canon",
                "location": "Chicago",
                "serialNum": "xyz456"
            }
        ]
    },
    {
        "date": "Sep 3, 2014",
        "devices": [
            {
                "model": "Canon",
                "location": "Chicago",
                "serialNum": "xyz456"
            },
            {
                "model": "Canon",
                "location": "Chicago",
                "serialNum": "stu789"
            },
            {
                "model": "Epson",
                "location": "NewYork",
                "serialNum": "123456"
            },
            {
                "model": "Epson",
                "location": "NewYork",
                "serialNum": "555555"
            },
            {
                "model": "HP",
                "location": "NewYork",
                "serialNum": "987654"
            }
        ]
    }
]

}

我想获取每个位置的唯一设备总数

Chicago - 4
New York - 3

【问题讨论】:

    标签: javascript json nested underscore.js lodash


    【解决方案1】:

    就是这样,在一个带有 lodash 链接语法的表达式中。简短的版本是将所有设备放入一个庞大的列表中,然后按位置对它们进行分组,然后为每个位置消除重复的 ID,然后对设备进行计数。

    _(dates) //Begin chain
        .pluck("devices") //get the device list for each date
        .flatten() //combine all device lists into a master list
        .groupBy("location") //group into an object of {"location": [devices]} pairs
        .mapValues(function (devicesForLocation) { //replaces [devices] with a count of number of unique serial numbers
            return _(devicesForLocation) //Begin chain
                .pluck("serialNum") //get the serial number for each device
                .uniq() //remove the duplicate serial numbers
            .value() //End chain
            .length; // get the count of unique serial numbers
        }) // result is an object of {"location": countOfUniqueDevices} pairs
    .value() //End chain
    

    最终结果是以下形式的对象:{"New York": 1, "NewYork": 3, "Chicago": 4},但显然您可以添加另一个语句以字符串形式将其打印出来。

    我鼓励您逐步完成此操作,以查看每个步骤的结果并了解其作用。

    【讨论】:

    • Retsam,你为什么使用 mapValues 而不仅仅是 map?
    • @devwannabe map 总是返回一个数组,但是在这种情况下我们不想要数组,我们想要一个对象; mapValues 根据传递给它的函数的结果返回具有相同键但具有不同值的对象。
    • 所以如果var foo = {x:1, y:2}; var plusOne = function(x) {return x+1},那么_.map(foo, plusOne)返回[2,3]_.mapValues(foo, plusOne)返回{x:2, y:3}
    • 太棒了!顺便说一句,我在玩你的解决方案。我通过在设备上方添加另一层来更改源数据。现在 pluck 找不到设备。我的印象是 pluck 会自动搜索设备。这是我的 jsfiddle 失败了 - jsfiddle.net/1gjr6hjv
    • 我通过在 .pluck('devices') 之前添加 pluck('locations').flatten() 使其工作 :)
    猜你喜欢
    • 2021-12-10
    • 2021-07-26
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多