我想要达到这样的目标:
设bigArray =[ key1 => [],key2 => [],key3 => [] ];
其中bigArray将是一个数组,在本例中包含3个具有特定名称/键的数组。我不想使用地图,我想以这种方式访问和管理数组值:
设array1 = bigArray.key1;
键1.推(“项目”);
我已经尝试了大约一个小时在互联网上寻找这种确切的情况,但我还没有找到方法来做这个准确的打字稿,所以我希望有人在typescript的实验者可以帮助我解决这个问题!非常感谢!
发布于 2022-03-04 05:12:25
我不知道您在寻找什么确切的用例,但是如果您想按键访问数组,则应该是一个映射。如果只是数组的数组,则必须使用索引来查找位置。地图允许您将数组分配给键,方便查找和搜索数据。下面是一个小例子:
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
map = new Map<String, []>();
constructor() {
this.map.set('sss', ['sss', 'asdasd']);
this.map.set('aaa', ['sss', '1123']);
this.map.set('sass', ['sss']);
this.map.set('xxx', ['sss']);
this.map.set('ss', ['sss']);
this.map.forEach((value: string, key: string) => {
console.log(key, value);
});
this.getKeys;
this.getValues;
this.getKey('xxx');
}
getKey(key: string) {
console.log('I am single key: ', this.map.get(key));
}
getKeys(map) {
console.log('I am array of keys: ', Array.from(map.keys()));
}
getValues(map) {
console.log('I am array of values: ', Array.from(map.values()));
}
}
工作示例:https://stackblitz.com/edit/angular-map-array-from-gqxdgu?file=app%2Fapp.component.ts
https://stackoverflow.com/questions/71344074
复制相似问题