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

上面的代码不起作用。

4

1 回答 1

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 );
}
于 2013-01-17T06:47:31.797 回答