[英]Create new object from existing object with new values Angular / Ionic 3
[英]Best way to create new object from existing object with certain values
從具有某些值的現有 object 中創建新的 object 的最佳方法是什么,我的意思是我有一個對象,我想創建一個只有我需要的值的新對象;
例如; 現有object
data = [{
"ID": "234324234",
"calldate": "2018-03-25",
"callend": "2018-03-25",
"duration": "00:32",
"connect_duration": "00:01",
"progress_time": "2",
"first_rtp_time": "2",
"caller": "3243242342",
"caller_domain": "XXX.XXXX.XXX",
}, {
"ID": "5675675",
"calldate": "2018-03-12",
"callend": "2018-03-12",
"duration": "00:45",
"connect_duration": "00:04",
"progress_time": "2",
"first_rtp_time": "2",
"caller": "878678865",
"caller_domain": "XXX.XXXX.XXX",
}]
需要新的 Object;
new_object = [{
"ID": "234324234",
"calldate": "2018-03-25",
"callend": "2018-03-25",
"caller": "3243242342",
}, {
"ID": "5675675",
"calldate": "2018-03-12",
"callend": "2018-03-12",
"caller": "878678865",
}]
你可以通過使用map
function 來做類似的事情
const new_object = data.map(({ ID, calldate, callend, caller }) => {
return { ID, calldate, callend, caller };
});
您可以使用class-transformer 。
你也可以看看plainToClass
的實現,自己實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.