Assembling commodity attributes and corresponding optional parameter array data

Source: Internet
Author: User

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

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.