使用Javascript从嵌套对象中获取唯一对象

时间:2016-07-26 15:45:36

标签: javascript underscore.js

我目前的对象如下

{
    "payload": [{
        "id": "idval1",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            }]
        }]
    },
    {
        "id": "idval2",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            },
            {
                "id": "1236",
                "value": "Three",
                "ddid": "dd3",
                "name": "third value"
            }]
        }]
    },
    {
        "id": "idval3",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            }]
        }]
    },
    {
        "id": "idval4",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            },
            {
                "id": "1236",
                "value": "Three",
                "ddid": "dd3",
                "name": "third value"
            }]
        }]
    },
    {
        "id": "idval5",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            },
            {
                "id": "1236",
                "value": "Three",
                "ddid": "dd3",
                "name": "third value"
            }]
        }]
    },
    {
        "id": "idval6",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            }]
        }]
    },
    {
        "id": "idval7",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            },
            {
                "id": "1236",
                "value": "Three",
                "ddid": "dd3",
                "name": "third value"
            }]
        }]
    }]
}

以下是我期望从当前对象

的对象集
{
    "payload": [{
        "id": "idval1,idval3,idval6",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            }]
        }]
    },
    {
        "id": "idval2,idval4,idval5,idval7",
        "type": [{
            "lists": [{
                "id": "1234",
                "value": "One",
                "ddid": "dd1",
                "name": "first value"
            },
            {
                "id": "1235",
                "value": "Two",
                "ddid": "dd2",
                "name": "second value"
            },
            {
                "id": "1236",
                "value": "Three",
                "ddid": "dd3",
                "name": "third value"
            }]
        }]
    }]
}

有没有办法使用下划线或JavaScript来实现这一点。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用Lodash,您可以使用_.uniqWith提供自定义比较器以查找唯一性。通过改变比较器中的第二个参数,您可以组合id s。



var obj = {"payload":[{"id":"idval1","type":[{"lists":[{"id":"1234","value":"One","ddid":"dd1","name":"first value"},{"id":"1235","value":"Two","ddid":"dd2","name":"second value"}]}]},{"id":"idval2","type":[{"lists":[{"id":"1234","value":"One","ddid":"dd1","name":"first value"},{"id":"1235","value":"Two","ddid":"dd2","name":"second value"},{"id":"1236","value":"Three","ddid":"dd3","name":"third value"}]}]},{"id":"idval3","type":[{"lists":[{"id":"1234","value":"One","ddid":"dd1","name":"first value"},{"id":"1235","value":"Two","ddid":"dd2","name":"second value"}]}]},{"id":"idval4","type":[{"lists":[{"id":"1234","value":"One","ddid":"dd1","name":"first value"},{"id":"1235","value":"Two","ddid":"dd2","name":"second value"},{"id":"1236","value":"Three","ddid":"dd3","name":"third value"}]}]},{"id":"idval5","type":[{"lists":[{"id":"1234","value":"One","ddid":"dd1","name":"first value"},{"id":"1235","value":"Two","ddid":"dd2","name":"second value"},{"id":"1236","value":"Three","ddid":"dd3","name":"third value"}]}]},{"id":"idval6","type":[{"lists":[{"id":"1234","value":"One","ddid":"dd1","name":"first value"},{"id":"1235","value":"Two","ddid":"dd2","name":"second value"}]}]},{"id":"idval7","type":[{"lists":[{"id":"1234","value":"One","ddid":"dd1","name":"first value"},{"id":"1235","value":"Two","ddid":"dd2","name":"second value"},{"id":"1236","value":"Three","ddid":"dd3","name":"third value"}]}]}]};

obj.payload = _.uniqWith(obj.payload, function(val, accVal) {
  if (_.isEqual(val.type, accVal.type)) {
    accVal.id += "," + val.id;
    return true;
  }
  return false;
});

console.log(obj);

<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>
&#13;
&#13;
&#13;