2016-10-19 45 views
0

我有這樣的對象數組。他們有重複屬性contactNamecategoryName由幾個屬性組成的對象數組,並帶有下劃線

[ 
    { 
     categoryId:1 
     categoryName:"Default" 
     contactName:"Anonymous" 
     name:"Mobile" 
     value:"+4417087654" 
    }, 
    { 
     categoryId:1 
     categoryName:"Default" 
     contactName:"Anonymous" 
     name:"Email" 
     value:"[email protected]" 
    }, 
    { 
     categoryId:1 
     categoryName:"Default" 
     contactName:"Test" 
     name:"Email" 
     value:"[email protected]" 
    }, 
    { 
     categoryId:1 
     categoryName:"Default" 
     contactName:"Test" 
     name:"Mobile" 
     value:"+4417087654" 
    }, 
    { 
     categoryId:2 
     categoryName:"Personal" 
     contactName:"Anonymous" 
     name:"Email" 
     value:"[email protected]" 
    }, 
    { 
     categoryId:2 
     categoryName:"Personal" 
     contactName:"Anonymous" 
     name:"Email" 
     value:"[email protected]" 
    }, 
    { 
     categoryId:2 
     categoryName:"Personal" 
     contactName:"Test" 
     name:"Email" 
     value:"[email protected]" 
    }, 
    { 
     categoryId:2 
     categoryName:"Personal" 
     contactName:"Test" 
     name:"Email" 
     value:"[email protected]" 
    } 
] 

我通過性能contactName的值要他們組和categoryName

這可能是這樣的

[ 
    { 
    "categoryId": 1, 
    "categoryName": "Default", 
    entity: [ 
    { 
     "contactName": "Anonymous", 
     "addresses": [ 
     { 
     "name": "Mobile", 
     "value": "+4417087654" 
     }, 
     { 
     "name": "Email", 
     "value": "[email protected]" 
     }, 
     { 
     "name": "Email", 
     "value": "[email protected]" 
     } 
    ] 
    }, 
    { 
     "contactName": "Test", 
     "addresses": [ 
     { 
     "name": "Mobile", 
     "value": "+4417087654" 
     }, 
     { 
     "name": "Email", 
     "value": "[email protected]" 
     }, 
     { 
     "name": "Email", 
     "value": "[email protected]" 
     } 
    ] 
    }] 

    }, 
    { 
    "categoryId": 2, 
    "categoryName": "Personal", 
    entity: [ 
    { 
     "contactName": "Anonymous", 
     "addresses": [ 
     { 
     "name": "Mobile", 
     "value": "+4417087654" 
     }, 
     { 
     "name": "Email", 
     "value": "[email protected]" 
     }, 
     { 
     "name": "Email", 
     "value": "[email protected]" 
     } 
    ] 
    }, 
    { 
     "contactName": "Test", 
     "addresses": [ 
     { 
     "name": "Mobile", 
     "value": "+4417087654" 
     }, 
     { 
     "name": "Email", 
     "value": "[email protected]" 
     }, 
     { 
     "name": "Email", 
     "value": "[email protected]" 
     } 
    ] 
    }] 

    } 

] 
+1

最終輸出應該怎麼樣子的呢? – gurvinder372

回答

1

在平凡的Javascript中,您可以爲嵌套組使用輔助對象。

var data = [{ categoryId: 1, categoryName: "Default", contactName: "Anonymous", name: "Mobile", value: "+4417087654" }, { categoryId: 1, categoryName: "Default", contactName: "Anonymous", name: "Email", value: "[email protected]" }, { categoryId: 1, categoryName: "Default", contactName: "Test", name: "Email", value: "[email protected]" }, { categoryId: 1, categoryName: "Default", contactName: "Test", name: "Mobile", value: "+4417087654" }, { categoryId: 2, categoryName: "Personal", contactName: "Anonymous", name: "Email", value: "[email protected]" }, { categoryId: 2, categoryName: "Personal", contactName: "Anonymous", name: "Email", value: "[email protected]" }, { categoryId: 2, categoryName: "Personal", contactName: "Test", name: "Email", value: "[email protected]" }, { categoryId: 2, categoryName: "Personal", contactName: "Test", name: "Email", value: "[email protected]" }], 
 
    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; }

+0

由於這是一個使用Underscore(或lodash)作爲依賴關係的Backbone問題,因此最好使用可用的工具。不是你的答案不正確! –

相關問題