除了一些属性之外,如何删除typescript中对象的所有属性

时间:2018-03-31 12:41:14

标签: angular sorting typescript arraylist iterator

我想删除typescript中对象的所有属性,除了我想要在对象中的一些属性。 我想要保留三个属性(start_time,纬度和海拔高度) 我的列表对象:

listObject= [
  {
    "start_time":"2018-03-24T08:12:11.706Z",
    "latitude":20.4841203,
    "longitude":77.8726691,
    "duration":0,
    "completed_by":0,
    "started_by":0
  },
  {
    "start_time":"2018-03-24T08:00:17.441Z",
    "latitude":20.4840866,
    "longitude":77.8726445,
     "altitude":272,
    "started_by":0
     "duration":10,
  },
  {
    "start_time":"2018-03-24T09:06:28.039Z",
    "latitude":20.4840631,
    "longitude":77.872614,
    "datastream":"ABC",
  },
  {
    "altitude":0,
    "latitude":0,
    "longitude":0,
    "start_time":"2018-03-28T11:26:16.332Z",
    "Average":60,
    "Name":"Navi",
    "Price":42699
  }
]

我只想跟随列表对象:

listObject= [
  {
    "start_time":"2018-03-24T08:12:11.706Z",
    "latitude":20.4841203,
    "longitude":77.8726691,
  },
  {
    "start_time":"2018-03-24T08:00:17.441Z",
    "latitude":20.4840866,
    "longitude":77.8726445,
  },
  {
    "start_time":"2018-03-24T09:06:28.039Z",
    "latitude":20.4840631,
    "longitude":77.872614,
  },
  {
    "altitude":0,
    "latitude":0,
    "longitude":0,
    "start_time":"2018-03-28T11:26:16.332Z",
  }
]

请帮帮我..

1 个答案:

答案 0 :(得分:3)

首先,您的 JSON 是错误的,它在对象属性上缺少一些 , 。以下是更正的 JSON

您还可以使用 array.map() 来迭代对象并使用您的属性创建新对象。

<强>样本

&#13;
&#13;
var listObject=[
  {
    "start_time": "2018-03-24T08:12:11.706Z",
    "latitude": 20.4841203,
    "longitude": 77.8726691,
    "duration": 0,
    "completed_by": 0,
    "started_by": 0
  },
  {
    "start_time": "2018-03-24T08:00:17.441Z",
    "latitude": 20.4840866,
    "longitude": 77.8726445,
    "altitude": 272,
    "started_by": 0,
    "duration": 10
  },
  {
    "start_time": "2018-03-24T09:06:28.039Z",
    "latitude": 20.4840631,
    "longitude": 77.872614,
    "datastream": "ABC"
  },
  {
    "altitude": 0,
    "latitude": 0,
    "longitude": 0,
    "start_time": "2018-03-28T11:26:16.332Z",
    "Average": 60,
    "Name": "Navi",
    "Price": 42699
  }
];

var newList = listObject.map(obj => ({start_time: obj.start_time, latitude: obj.latitude, longitude: obj.longitude}));

console.log(newList);
&#13;
&#13;
&#13;