我有一个以下格式的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 :(得分: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 );
}