Extracting the eigenvalues of an object in an array to create a new object implementation method

Source: Internet
Author: User

A visual SQL Statement generation feature view has recently been made

The project encountered a JSON save after the restoration of the problem, because the save before and the background communication Convention to save the JSON format, so in the restoration, you need to follow the saved format for reverse parsing.

First look at the saved JSON content

var oldobj= [{"FieldName": "Office hours 1",        "FieldID": "Working_hours_1",        "FieldType": "Time",        "Parenttableid": "Class_table",        "Parenttablename": "Class Table",        "Order": 0,        "Checked":false      },      {        "FieldName": "Time off 1",        "FieldID": "Off_hours_1",        "FieldType": "Time",        "Parenttableid": "Class_table",        "Parenttablename": "Class Table",        "Order": 0,        "Checked":false      },      {        "FieldName": "Office Hours 2",        "FieldID": "Working_hours_2",        "FieldType": "Time",        "Parenttableid": "Class_table",        "Parenttablename": "Class Table",        "Order": 0,        "Checked":false      },      {        "FieldName": "Time off 2",        "FieldID": "Off_hours_2",        "FieldType": "Time",        "Parenttableid": "Class_table",        "Parenttablename": "Class Table",        "Order": 0,        "Checked":false      },      {        "FieldName": "Name",        "FieldID": "Name",        "FieldType": "Text",        "Parenttableid": "Punch_card_record",        "Parenttablename": "Punch card Record",        "Order": 0,        "Checked":false      },      {        "FieldName": "Work No.",        "FieldID": "Job_number",        "FieldType": "Text",        "Parenttableid": "Punch_card_record",        "Parenttablename": "Punch card Record",        "Order": 0,        "Checked":false      },      {        "FieldName": "Punch-In Time",        "FieldID": "Punch_time",        "FieldType": "DateTime",        "Parenttableid": "Punch_card_record",        "Parenttablename": "Punch card Record",        "Order": 0,        "Checked":false      }    ],

Note: Please ignore the order,checked field in the object.

Let's say that each obj in the oldobj array is called a field , and you can see that the Parenttableid of the field is duplicated.

And we're going to parse this array into a new object with Parenttableid as its eigenvalues.

var newObj = [{"Parentformid": "Class_table",        "Parentformname": "Class Table",        "Fields": [            {                "FieldName": "Office hours 1",                "FieldID": "Working_hours_1",                "FieldType": "Time",                "Parenttableid": "Class_table",                "Parenttablename": "Class Table",                "Order": 0,                "Checked":false            },            {                "FieldName": "Time off 1",                "FieldID": "Off_hours_1",                "FieldType": "Time",                "Parenttableid": "Class_table",                "Parenttablename": "Class Table",                "Order": 0,                "Checked":false            },            {                "FieldName": "Office Hours 2",                "FieldID": "Working_hours_2",                "FieldType": "Time",                "Parenttableid": "Class_table",                "Parenttablename": "Class Table",                "Order": 0,                "Checked":false            },            {                "FieldName": "Time off 2",                "FieldID": "Off_hours_2",                "FieldType": "Time",                "Parenttableid": "Class_table",                "Parenttablename": "Class Table",                "Order": 0,                "Checked":false            }        ]    },    {        "Parentformid": "Punch_card_record",        "Parentformname": "Punch card Record",        "Fields": [            {                "FieldName": "Name",                "FieldID": "Name",                "FieldType": "Text",                "Parenttableid": "Punch_card_record",                "Parenttablename": "Punch card Record",                "Order": 0,                "Checked":false            },            {                "FieldName": "Work No.",                "FieldID": "Job_number",                "FieldType": "Text",                "Parenttableid": "Punch_card_record",                "Parenttablename": "Punch card Record",                "Order": 0,                "Checked":false            },            {                "FieldName": "Punch-In Time",                "FieldID": "Punch_time",                "FieldType": "DateTime",                "Parenttableid": "Punch_card_record",                "Parenttablename": "Punch card Record",                "Order": 0,                "Checked":false            }        ]    }]

Note: Please ignore the Parenttableidin the new object,parenttablename,order,checked field, because it does not affect our purpose, in order to facilitate the direct push in the object.

You can see that in the new array object, the Id,name of the parenttable in the field are extracted, and the fields are push into a newobj array.
So the result of this implementation is how it is achieved, and look at the code:

//create a new array to hold the eigenvaluesLet Parenttableidarray = [];//traversing old array objects for(Let item of Oldobj) {//gets the current object Parenttableid whether there is an array of eigenvaluesLet index =Parenttableidarray.indexof (Item.parenttableid)//current Object Parenttableid when no eigenvalues array exists  if(Index = =-1){//joins the current object Parenttableid to the eigenvalues arrayParentformidarray.push (Item.parenttableid)//To create a new objectLet obj ={fields: [], ParentFormId:item.parentTableId, parentFormName:item.parentTableName} Obj.field S.push (item);//inserting a newly generated object     This. Newobj.push (obj); }//when an array of eigenvalues exists for the current object Parenttableid  Else{//inserts a new object at the location of the current object Parenttableid     This. newobj[index][' Fields '].push (item); }}   

This method can effectively extract the object eigenvalues within the array. If multiple calls are required, consider encapsulation.

Extracting the eigenvalues of an object in an array to create a new object implementation method

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.