我有一个像这样的对象数组
[
{bookName:"javascript",authorName:"john"},
{bookName:"java",authorName:"peter"},
{bookName:"j2ee",authorName:"john"},
{bookName:".net",authorName:"peter"},
{bookName:"oracle",authorName:"peter"}];
我尝试了逻辑,但我没有正确。我想这样整理..
{
peter:{
[{bookName:"java",authorName:"peter"},
{bookName:".net",authorName:"peter"},
{bookName:"oracle",authorName:"peter"} ]
} ,
john :{
[{bookName:"j2ee",authorName:"john"},
{bookName:"javascript",authorName:"john"}]
}
}.
我该怎么做..提前谢谢..
var obj={};
var objArr = given objectarray;
for(var i=0;i<objArr.length.i++ ){
objArr[objArr[i].authorName]=objArr[i];
}
和
var obj={};
var objArr = given objectarray;
for(var i=0;i<objArr.length.i++ ){
var arr=[];
objArr[objArr[i].authorName]=arr.push(objArr[i]);
}