如何从打字稿中的对象中删除一些属性?

时间:2017-08-04 06:21:59

标签: typescript arraylist angular2-services

假设我按如下方式创建了一个对象:

formSummery: any = [];

{
  "jobs" : [ {
    "job_id" : 10,
    "users" : [ {
      "user_id" : 11,
      "data_points" : [ {
        "efficiency" : "good",
        "form_id" : "2",
        "instrument_type" : "plug",
        "background_value" : "7",
        "surveyed_item_id" : "100",
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "abc@gmail.com.com"
      }]
    } ]
  } ]
}

删除属性数据流,用户名和start_time以最终获得新的formSummery的最佳方法是什么?

{
  "jobs" : [ {
    "job_id" : 10,
    "users" : [ {
      "user_id" : 11,
      "data_points" : [ {
        "efficiency" : "good",
        "form_id" : "2",
        "instrument_type" : "plug",
        "background_value" : "7",
        "surveyed_item_id" : "100",
        "duration" : 0
      }]
    } ]
  } ]
}

现在,我有两个相同的数组formSummery和unstrdformSummery,我想删除第二个数组(unstrdformSummery)中的一些属性,并且这些属性仍然需要第一个数组(formSummery):

formSummery = 
{ 
    "planned_activity":"Yes",
    "ingest_time":"2017-08-03T12:12:32.456Z",
    "arrival_time":"8",
    "device_id":"26e09c88237d5342",
    "user_name":"ABC","form_id":"1",
    "firmware_version":"2.2",
    "site_access_was_provided_as_expected":"Yes",
    "energy_manager":"Aaaa",
    "duration":0,
    "start_time":"2017-08-03T12:12:28.736Z",
    "datastream":"GENERIC",
    "addressing_percent_complete":"92",
    "password":"pwd",
    "template_name":"Daily Field Report",
    "user_id":11,
    "site_access_notes":"testing",
    "startup_percent_complete":"65",
    "energy_manager_ip":"0:0:0:0",
    "was_site_access_granted_on_time":"Yes",
    "departure_time":"9",
    "username":"abc@gmail.com"
}


unstrdformSummery =
{
    "planned_activity":"Yes",
    "ingest_time":"2017-08-03T12:12:32.456Z",
    "arrival_time":"8",
    "device_id":"26e09c88237d5342",
    "user_name":"ABC","form_id":"1",
    "firmware_version":"2.2",
    "site_access_was_provided_as_expected":"Yes",
    "energy_manager":"Aaaa",
    "duration":0,
    "start_time":"2017-08-03T12:12:28.736Z",
    "datastream":"GENERIC",
    "addressing_percent_complete":"92",
    "password":"pwd",
    "template_name":"Daily Field Report",
    "user_id":11,
    "site_access_notes":"testing",
    "startup_percent_complete":"65",
    "energy_manager_ip":"0:0:0:0",
    "was_site_access_granted_on_time":"Yes",
    "departure_time":"9",
    "username":"abc@gmail.com"
}

1 个答案:

答案 0 :(得分:2)

嗯,你可以通过调用简单的delete函数来实现:

var myObject = {
    "jobs": [{
        "job_id": 10,
        "users": [{
            "user_id": 11,
            "data_points": [{
                "efficiency": "good",
                "form_id": "2",
                "instrument_type": "plug",
                "background_value": "7",
                "surveyed_item_id": "100",
                "duration": 0,
                "start_time": "2017-07-20T04:04:43.000Z",
                "datastream": "GENERIC",
                "username": "abc@gmail.com.com"
            }]
        }]
    }]
};

for (let i = 0; i < myObject.jobs.length; i++){
    for (let j = 0; j < myObject.jobs[i].users.length; j++){
        for (let g = 0; g < myObject.jobs[i].users[j].data_points.length; g++){
            delete myObject.jobs[i].users[j].data_points[g].username;
            delete myObject.jobs[i].users[j].data_points[g].datastream;
            delete myObject.jobs[i].users[j].data_points[g].start_time;
        }
    }
}

如果它也只有1个用户,1个作业,1个data_points,你可以这样使用:

delete myObject.jobs[0].users[0].data_points[0].username;
delete myObject.jobs[0].users[0].data_points[0].datastream;
delete myObject.jobs[0].users[0].data_points[0].start_time;

------ EDIT

如果要存储包含所有参数的旧对象,可以在删除之前克隆对象。您可以使用Object.clone函数或只使用JSON.parse(JSON.stringify(object))(我在示例中使用它)。你不能使用简单的var newObj = object(如果你改变对象,newobj也会改变)

formSummery = 
{ 
    "planned_activity":"Yes",
    "ingest_time":"2017-08-03T12:12:32.456Z",
    "arrival_time":"8",
    "device_id":"26e09c88237d5342",
    "user_name":"ABC","form_id":"1",
    "firmware_version":"2.2",
    "site_access_was_provided_as_expected":"Yes",
    "energy_manager":"Aaaa",
    "duration":0,
    "start_time":"2017-08-03T12:12:28.736Z",
    "datastream":"GENERIC",
    "addressing_percent_complete":"92",
    "password":"pwd",
    "template_name":"Daily Field Report",
    "user_id":11,
    "site_access_notes":"testing",
    "startup_percent_complete":"65",
    "energy_manager_ip":"0:0:0:0",
    "was_site_access_granted_on_time":"Yes",
    "departure_time":"9",
    "username":"abc@gmail.com"
}

newForm = JSON.parse(JSON.stringify(formSummery));


delete newForm.username;
delete newForm.datastream;
delete newForm.start_time;

newForm对象将存储没有这些参数的新对象,而formSummery将存储旧对象(所有参数)。

我希望它有所帮助。