1 vararr = [{2"id": 1,3"Name": "Memory",4"Sid": 1,5"Sname": "16G"6 }, {7"id": 1,8"Name": "Memory",9"Sid": 2,Ten"Sname": "32G" One }, { A"ID": 2, -"Name": "Color", -"Sid": 5, the"Sname": "White" - }, { -"ID": 2, -"Name": "Color", +"Sid": 6, -"Sname": "Gold" + }, { A"ID": 2, at"Name": "Color", -"Sid": 7, -"Sname": "Yellow" -}];
Today do mall backstage management system, the background to pass over the format of data. Presumably, a property of a commodity, such as "memory", corresponds to n specific values. I need to assemble the data into the following format:
{ "1": { "id": "1", "Name": "Memory", "Res": [{ "id": 1, "Name": "Memory", "Sid": 1, "Sname": "16G" }, { "id": 1, "Name": "Memory", "Sid": 2, "Sname": "32G" }] }, "2": { "id": "2", "Name": "Color", "Res": [{ "ID": 2, "Name": "Color", "Sid": 5, "Sname": "White" }, { "ID": 2, "Name": "Color", "Sid": 6, "Sname": "Gold" }, { "ID": 2, "Name": "Color", "Sid": 7, "Sname": "Yellow" }] }}
This allows me to iterate through the array data in the Res property of each property in the for-in traversal of the large object, and finally each row shows data in the format "color: white, gold, yellow".
Recently the brain is not enough, and finally under the guidance of the manager, successfully wrote out, the method is as follows:
varOutobj = {}; for(vari = 0, len = arr.length; i < Len; i++) { varTMP =Arr[i]; if(!Outobj.hasownproperty (tmp.sku_param_id)) {outobj[tmp.sku_param_id]= {}; varInobj = {}; varInarr = []; inobj["id"] =tmp.sku_param_id; inobj["Name"] =Tmp.param_web_name; Inarr.push (TMP); inobj["res"] =Inarr; OUTOBJ[TMP.SKU_PARAM_ID]=Inobj; } Else { varInobj =outobj[tmp.sku_param_id]; varInarr = inobj["res"]; Inarr.push (TMP); inobj["res"] =Inarr; }}
Assembling commodity attributes and corresponding optional parameter array data