欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

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

来自  https://blog.csdn.net/weixin_44917045/article/details/111591211


普通分类: