<script>
var examviewlist = [{
"Examitemids": 221,
"Timepoint": "20"
}, {
"Examitemids": 240,
"Timepoint": "40"
}, {
"Examitemids": 221,
"Timepoint": "30"
}, {
"Examitemids": 220,
"Timepoint": "30"
}, {
"Examitemids": 222,
"Timepoint": "40"
}, {
"Examitemids": 222,
"Timepoint": "40"
}
];
/**
*
* Parameters: Obj Incoming object
* Mainkey PRIMARY key
* Key Value name to merge
* Does the flag merge need to be re-
*/
function Objswitch (obj, Mainkey, Key,flag) {
if (typeof array.indexof!== ' function ') {
Array.prototype.indexOf = function (args) {
var index =-1;
for (var i=0,l=this.length; i<l; i++) {
if (this[i] = = = args) {
index = i;
Break
}
}
return index;
}
}
var robj = []; The returned array
var robjhasmainkey = []; Returns the primary key value that the array contains
var i = obj.length;
while (Obj[--i]) {
var rindex = Robjhasmainkey.indexof (Obj[i][mainkey]);
if (Rindex = =-1) {
var tempobj={};
Robjhasmainkey.push (Obj[i][mainkey]);
Tempobj[mainkey] = Obj[i][mainkey];
Tempobj[key] = [Obj[i][key]];
Robj.push (Tempobj);
}else{//if it already exists, write directly to the returned obj
if (flag==true) {
if (Robj[rindex][key].indexof (Obj[i][key]) ==-1) {
Robj[rindex][key].push (Obj[i][key]);
}
}else{
Robj[rindex][key].push (Obj[i][key]);
}
}
}
Console.log (RobJ);
}
Objswitch (examviewlist, ' timepoint ', ' examitemids ')
</script>
The inside of the array is combined with the key values