Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何以角度更新常量数组?_Javascript_Html_Angular - Fatal编程技术网

Javascript 如何以角度更新常量数组?

Javascript 如何以角度更新常量数组?,javascript,html,angular,Javascript,Html,Angular,亲爱的团队成员: 我是个新手 我有一个ts文件 export const HEROES: Hero[] = [ { id: 11, name: 'Dr Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombasto' }, { id: 14, name: 'Celeritas' }, { id: 15, name: 'Magneta' }, { id: 16, name: 'RubberMan' }, { i

亲爱的团队成员:

我是个新手

我有一个ts文件

export const HEROES: Hero[] = [
  { id: 11, name: 'Dr Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];


updateHero(hero:Hero) : void{
  let updateItem = this.heroes.find(x=> x.id == hero.id);

  let index = this.heroes.indexOf(updateItem);

  this.heroes[index] = hero;
}
如何更新英雄文件

我的目标是选择一个英雄,编辑名字,然后更新const文件。我知道它应该来自服务器。但是为了玩乐


谢谢。

一旦常量声明,您就不能分配/更新它的值。因此,您需要将const类型更改为其他类型

默认情况下,javascript常量行为

用于获取索引并更新该索引的值例如id索引为11,名称为Dr Shugh,我的最终结果是否可以导出如下文件:Hero[]=[{id:11,名称:'Dr Shugh'},{id:12,名称:'Narco'},{id:13,名称:'Bombasto'},{id:14,名称:'Celeritas'},{id:15,姓名:'Magneta'},{id:16,姓名:'RubberMan'},{id:17,姓名:'Dynama'},{id:18,姓名:'Dr IQ'},{id:19,姓名:'Magma'},{id:20,姓名:'Tornado'}];请注意,在中导入的sperate ts文件中存在此类component@CodeManiac任何情况下,这都是绝对错误的。你不能重新分配
常量
。你绝对可以改变它所引用的对象。此外,别担心,这是一个垃圾存储库