How can I get unique array from a collection of nested objects with lodash?

混江龙づ霸主 提交于 2019-12-11 10:55:13

问题


I have the following collection:

"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",
            }
] 

I want to grab unique grapeTypes from that collection. The returning array shold be ["Chardonnay","Espadeiro"]

What is the best way to do it with lodash?


回答1:


I think this combination of pluck, map and filter should do it:

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);

Check the demo run below.

// 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. If grapeType can be missing from properties then the script should be

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



回答2:


Here's one way to do it with lodash:

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

First, you get the properties arrays using pluck(). Next, you use find() to get the first object in this array that has a grapeType property. This is done using has(), and partially-applying the argument to build the callback function.

Next, you use pluck() again to get the actual property values. Finally, uniq() ensures there are no duplicates.



来源:https://stackoverflow.com/questions/31113430/how-can-i-get-unique-array-from-a-collection-of-nested-objects-with-lodash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!