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);

你可能感兴趣的:(TypeScript)