2013-01-17 106 views
0

我有以下格式的JavaScript對象,我想它歸類歸類數據散列映射

var inputFacetArray=[ 
    { category: 
"Company" 
, name: 
"nokia" 
}, { category: 
"Persons" 
, name: 
"ben affleck" 
}, { category: 
"Persons" 
, name: 
"daniel craig" 
}, { category: 
"Persons" 
, name: 
"george clooney" 
}] 

我想獲得它的輸出格式

[{Persons:["george clooney", "daniel craig", "ben affleck"]}, {company:["nokia"]}] 

我的代碼如下

var facetCategorizedArray=[]; 
for(var k=0; k<inputFacetArray.length; k++){ 
    var l={}; 
    inputFacetArray[k]["name"].push(l[inputFacetArray[k].category]); 
    facetCategorizedArray.push(l); 
} 

上面的代碼無法正常工作。

回答

0

這是否適合您?

var outputArray = {"Persons":[], "Company":[]}; 
for (var counter = 0; counter < inputFacetArray.length; counter++) 
{ 
    outputArray.Persons.push(inputFacetArray[counter].category); 
    outputArray.Company.push(inputFacetArray[counter].name); 
}