The inside of the array is combined with the key values

Source: Internet
Author: User

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.