使用下划线将多个属性分组对象数组

时间:2016-10-19 13:00:09

标签: javascript backbone.js underscore.js

我有这样的对象数组。他们有重复的属性contactNamecategoryName

[
    {
        categoryId:1
        categoryName:"Default"
        contactName:"Anonymous"
        name:"Mobile"
        value:"+4417087654"
    },
    {
        categoryId:1
        categoryName:"Default"
        contactName:"Anonymous"
        name:"Email"
        value:"test2@gmail.com"
    },
    {
        categoryId:1
        categoryName:"Default"
        contactName:"Test"
        name:"Email"
        value:"ivdtest@test.com"
    },
    {
        categoryId:1
        categoryName:"Default"
        contactName:"Test"
        name:"Mobile"
        value:"+4417087654"
    },
    {
        categoryId:2
        categoryName:"Personal"
        contactName:"Anonymous"
        name:"Email"
        value:"ivdtest@test.com"
    },
    {
        categoryId:2
        categoryName:"Personal"
        contactName:"Anonymous"
        name:"Email"
        value:"ivdtest@test.com"
    },
    {
        categoryId:2
        categoryName:"Personal"
        contactName:"Test"
        name:"Email"
        value:"ivdtest@test.com"
    },
    {
        categoryId:2
        categoryName:"Personal"
        contactName:"Test"
        name:"Email"
        value:"ivdtest2@test.com"
    }
]

我想按照属性contactNamecategoryName

的值对它们进行分组

可能是这样的

[
  {
    "categoryId": 1,
    "categoryName": "Default",
    entity: [
    {
      "contactName": "Anonymous",
      "addresses": [
      {
        "name": "Mobile",
        "value": "+4417087654"
      },
      {
        "name": "Email",
        "value": "test2@gmail.com"
      },
      {
        "name": "Email",
        "value": "ivdtest@test.com"
      }
    ]
    },
    {
      "contactName": "Test",
      "addresses": [
      {
        "name": "Mobile",
        "value": "+4417087654"
      },
      {
        "name": "Email",
        "value": "test2@gmail.com"
      },
      {
        "name": "Email",
        "value": "ivdtest@test.com"
      }
    ]
    }]

  },
  {
    "categoryId": 2,
    "categoryName": "Personal",
    entity: [
    {
      "contactName": "Anonymous",
      "addresses": [
      {
        "name": "Mobile",
        "value": "+4417087654"
      },
      {
        "name": "Email",
        "value": "test2@gmail.com"
      },
      {
        "name": "Email",
        "value": "ivdtest@test.com"
      }
    ]
    },
    {
      "contactName": "Test",
      "addresses": [
      {
        "name": "Mobile",
        "value": "+4417087654"
      },
      {
        "name": "Email",
        "value": "test2@gmail.com"
      },
      {
        "name": "Email",
        "value": "ivdtest@test.com"
      }
    ]
    }]

  }

]

2 个答案:

答案 0 :(得分:2)

var output = _.groupBy(input, function(entry) { 
  return entry.categoryName + ',' + entry.contactName; 
});

https://jsfiddle.net/9ttg1t3z/

答案 1 :(得分:1)

在普通的Javascript中,您可以为嵌套组使用辅助对象。

var data = [{ categoryId: 1, categoryName: "Default", contactName: "Anonymous", name: "Mobile", value: "+4417087654" }, { categoryId: 1, categoryName: "Default", contactName: "Anonymous", name: "Email", value: "test2@gmail.com" }, { categoryId: 1, categoryName: "Default", contactName: "Test", name: "Email", value: "ivdtest@test.com" }, { categoryId: 1, categoryName: "Default", contactName: "Test", name: "Mobile", value: "+4417087654" }, { categoryId: 2, categoryName: "Personal", contactName: "Anonymous", name: "Email", value: "ivdtest@test.com" }, { categoryId: 2, categoryName: "Personal", contactName: "Anonymous", name: "Email", value: "ivdtest@test.com" }, { categoryId: 2, categoryName: "Personal", contactName: "Test", name: "Email", value: "ivdtest@test.com" }, { categoryId: 2, categoryName: "Personal", contactName: "Test", name: "Email", value: "ivdtest2@test.com" }],
    result = [];

data.forEach(function (a) {
    if (!this[a.categoryName]) {
        this[a.categoryName] = { _: [] };
        result.push({ categoryId: a.categoryId, categoryName: a.categoryName, entity: this[a.categoryName]._ });
    }
    if (!this[a.categoryName][a.contactName]) {
        this[a.categoryName][a.contactName] = [];
        this[a.categoryName]._.push({ contactName: a.contactName, addresses: this[a.categoryName][a.contactName] });
    }
    this[a.categoryName][a.contactName].push({ name: a.name, value: a.value });
}, Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }