【问题标题】:How can I get unique array from a collection of nested objects with lodash?如何从带有 lodash 的嵌套对象集合中获取唯一数组?
【发布时间】:2015-06-29 10:31:48
【问题描述】:

我有以下收藏:

"items": [{
                "id": 1,
                "title": "Montrachet",
                "imageUrl": "http://winebuff.com.hk/products_image/3376-Ramonet-ChassagneMontrachetBlanc.jpg",
                "imageUrls": [
                    "http://winebuff.com.hk/products_image/3376-Ramonet-ChassagneMontrachetBlanc.jpg",
                    "http://media.riepenau.com/wines/17973_b.jpg",
                    "http://lorempixel.com/400/400/food/3"         
                ],
                "properties": [
                    {"description" : "Kırmızı Şaraplar Desc"},
                    {"region" :"Bordeaux"},
                    {"age": "16"},
                    {"producer" :"Kayra"},
                    {"grapeType":"Espadeiro"}

                ],
                "priceGlass": "1",
                "priceBottle": "2",
                "year": "1999"

            },

{
                "id": 2,
                "title": "Montrachet2",
                "imageUrl": "http://winebuff.com.hk/products_image/3376-Ramonet-ChassagneMontrachetBlanc.jpg",
                "imageUrls": [
                    "http://winebuff.com.hk/products_image/3376-Ramonet-ChassagneMontrachetBlanc.jpg",
                    "http://media.riepenau.com/wines/17973_b.jpg",
                    "http://lorempixel.com/400/400/food/3"         
                ],
                "properties": [
                    {"description" : "Kırmızı Şaraplar Desc"},
                    {"region" :"Bordeaux"},
                    {"age": "16"},
                    {"producer" :"Kayra"},
                    {"grapeType":"Chardonnay"}

                ],
                "priceGlass": "1",
                "priceBottle": "2",
                "year": "1999",
            }
] 

我想从该集合中获取独特的葡萄类型。返回的数组应该是 ["Chardonnay","Espadeiro"]

使用 lodash 的最佳方法是什么?

【问题讨论】:

  • lodash 有方法 _.uniq 可以在数组中找到唯一性。这将单独工作,因此您需要合并properties,然后将_.uniq 与关键字grapeType 应用。

标签: angularjs lodash


【解决方案1】:

我认为这种 pluck、map 和 filter 的组合应该可以做到:

var result = _.chain(obj.items).pluck('properties').map(function(obj) {
    return _.filter(obj, function(prop) {
        return prop.grapeType;
    })[0].grapeType;
}).uniq().value();

console.log(result);

检查下面的演示运行。

// Code goes here

var obj = {
    items: [{
            "id": 1,
            "title": "Montrachet",
            "imageUrl": "http://winebuff.com.hk/products_image/3376-Ramonet-ChassagneMontrachetBlanc.jpg",
            "imageUrls": [
                "http://winebuff.com.hk/products_image/3376-Ramonet-ChassagneMontrachetBlanc.jpg",
                "http://media.riepenau.com/wines/17973_b.jpg",
                "http://lorempixel.com/400/400/food/3"
            ],
            "properties": [{
                    "description": "Kırmızı Şaraplar Desc"
                }, {
                    "region": "Bordeaux"
                }, {
                    "age": "16"
                }, {
                    "producer": "Kayra"
                }, {
                    "grapeType": "Espadeiro"
                }

            ],
            "priceGlass": "1",
            "priceBottle": "2",
            "year": "1999"

        },

        {
            "id": 2,
            "title": "Montrachet2",
            "imageUrl": "http://winebuff.com.hk/products_image/3376-Ramonet-ChassagneMontrachetBlanc.jpg",
            "imageUrls": [
                "http://winebuff.com.hk/products_image/3376-Ramonet-ChassagneMontrachetBlanc.jpg",
                "http://media.riepenau.com/wines/17973_b.jpg",
                "http://lorempixel.com/400/400/food/3"
            ],
            "properties": [{
                    "description": "Kırmızı Şaraplar Desc"
                }, {
                    "region": "Bordeaux"
                }, {
                    "age": "16"
                }, {
                    "producer": "Kayra"
                }, {
                    "grapeType": "Chardonnay"
                }

            ],
            "priceGlass": "1",
            "priceBottle": "2",
            "year": "1999",
        }
    ]
};


var result = _.chain(obj.items).pluck('properties').map(function(obj) {
    return _.filter(obj, function(prop) {
        return prop.grapeType;
    })[0].grapeType;
}).uniq().value();

document.write(JSON.stringify(result));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.8.0/lodash.js"></script>

UPD。如果grapeType 可能会从properties 中丢失,那么脚本应该是

var result = _.chain(obj.items).pluck('properties').map(function(obj) {
    return (_.filter(obj, function(prop) {
        return prop.grapeType;
    })[0] || {}).grapeType;
}).compact().uniq().value();

【讨论】:

  • 感谢您的回答,它有效。我想知道如果我们没有任何“属性”数组,并且如果我们将这样的葡萄类型放在字段中会怎样; “priceGlass”:“1”,“priceBottle”:“2”,“grapeType”:“Chardonney”
  • 那么就是_.chain(obj.items).pluck('grapeType').uniq().value()
  • 谢谢!最后一个问题;如果我有这个“属性”数组并且其中没有葡萄类型字段,我该如何克服这个错误消息说“过滤器(...)[0] 未定义”?
【解决方案2】:

这是使用 lodash 的一种方法:

_(items)
    .pluck('properties')
    .map(function(item) {
        return _.find(item, _.ary(_.partialRight(_.has, 'grapeType'), 1));
    })
    .pluck('grapeType')
    .uniq()
    .value();

首先,您使用pluck() 获得properties 数组。接下来,您使用find() 获取此数组中具有grapeType 属性的第一个对象。这是使用has() 完成的,并部分应用参数来构建回调函数。

接下来,您再次使用pluck() 来获取实际的属性值。最后,uniq() 确保没有重复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多