TypeScript 对象动态添加属性和删除属性

1、准备对象数组

let cities = [
    {  id: 1, chineseName: '合肥', englishName: 'Hefei', area: '11445.1 km²', population: '818.9 万'},
    {  id: 2, chineseName: '南京', englishName: 'Nanjing', area: '6587.02 km²'},
];

2、添加属性

let findCity = cities.find(city => city.id === 2);
findCity['population'] = '850.0 万';
console.log(findCity);

3、删除属性

let findCity = cities.find(city => city.id === 1);
delete findCity.population;
console.log(findCity);
    原文作者:Horizons-code
    原文地址: https://blog.csdn.net/weixin_44917045/article/details/111591211
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞