如何从对象初始化关联数组
How to initialize an associative array from an object?
给定
this.themes = [
{
name: 'Material Teal',
colors: {
'primary': { default: [ 0, 150, 136], a100: [167, 255, 237], a200: [100, 255, 218], a400: [ 29, 233, 182], a700: [ 0, 191, 165] },
'accent' : { default: [103, 58, 183], a100: [179, 136, 255], a200: [124, 77, 255], a400: [101, 31, 255], a700: [ 98, 0, 234] },
'warn' : { default: [244, 67, 54], a100: [255, 138, 128], a200: [255, 82, 82], a400: [255, 23, 68], a700: [213, 0, 0] }
}
},
...
];
和
export class Theme {
name: string;
colors: { [intention: string]: { default: number[], a100: number[], a200: number[], a400: number[], a700: number[] } };
constructor(name: string, colors: { [intention: string]: { default: number[], a100: number[], a200: number[], a400: number[], a700: number[] } }) {
this.name = name;
this.colors = colors;
}
...
}
如何传入对象的colors
部分?我以为
let theme = new Theme(theme.name, theme.colors);
会解决问题,但这给了我一个错误:
[ts] Argument of type '{ 'primary': { default: number[]; a100: number[]; a200: number[]; a400: number[]; a700: number[];...' is not assignable to parameter of type '{ [intention: string]: { default: number[]; a100: number[]; a200: number[]; a400: number[]; a700:...'.
Index signature is missing in type '{ 'primary': { default: number[]; a100: number[]; a200: number[]; a400: number[]; a700: number[];...'.
什么是索引签名,我该怎么做?
发生这种情况是因为编译器不了解this.themes
属于以下类型:
{
name: string;
colors: { [name: string]: {
default: number[];
a100: number[];
a200: number[];
a400: number[];
a700: number[];
}
}
您需要做的就是指定类型,如下所示:
interface ThemeColor {
default: number[];
a100: number[];
a200: number[];
a400: number[];
a700: number[];
}
interface ThemeColors {
[name: string]: ThemeColor;
}
interface Theme {
name: string;
colors: ThemeColors;
}
let themes: Theme[] = [
{
name: 'Material Teal',
colors: {
'primary': { default: [ 0, 150, 136], a100: [167, 255, 237], a200: [100, 255, 218], a400: [ 29, 233, 182], a700: [ 0, 191, 165] },
'accent' : { default: [103, 58, 183], a100: [179, 136, 255], a200: [124, 77, 255], a400: [101, 31, 255], a700: [ 98, 0, 234] },
'warn' : { default: [244, 67, 54], a100: [255, 138, 128], a200: [255, 82, 82], a400: [255, 23, 68], a700: [213, 0, 0] }
}
}
];
export class ThemeClass {
name: string;
colors: ThemeColors;
constructor(data: Theme) {
this.name = data.name;
this.colors = data.colors;
}
}
new ThemeClass(themes[0]);
(操场上的代码)
尝试将[intention: string]
替换为'primary'
,如下所示:
export class Theme {
name: string;
colors: { 'primary': { default: number[], a100: number[], a200: number[], a400: number[], a700: number[] } };
constructor(name: string, colors: { 'primary': { default: number[], a100: number[], a200: number[], a400: number[], a700: number[] } }) {
this.name = name;
this.colors = colors;
}
...
}
和警告 !您正在使用 let
运算符定义theme
,但此变量已经存在!
相关文章:
- 多维关联数组的最后一个索引
- node.js/javascript/couchdb视图到关联数组似乎不起作用
- 具有关联数组数据集的D3.js表
- 后关联数组使用jquery ajax
- 返回一个关联数组,而不是mongoose中的文档数组
- 如何在 Angular JS 中从关联数组创建多个复选框
- 存储在变量中的关联数组中的访问键
- 如何在具有相同 ID 的 Javascript 中按排序函数对关联数组进行排序
- 在主要的JavaScript引擎中,在JavaScript关联数组(动态对象属性)中检索/插入的复杂性是多少
- 关联数组,并使用父数组d作为selection.data()的源数组
- 获取关联数组的标签
- JavaScript:如何在最接近值的关联数组中查找键
- 单击时显示关联数组的元素
- 关联数组 - 使用图像滑块显示键值
- Javascript从关联数组中查找最低数字(气泡排序方法)
- AngularJS过滤器不适用于我的数据:关联数组
- 获取关联数组中当前函数的键
- 如何按一个仍保持字母顺序的特性值(另一个特性值)对关联数组进行排序
- 正在检查JavaScript中是否存在关联数组
- 将数据附加到关联数组或维度数组