2 回答

TA贡献1877条经验 获得超1个赞
这是可能的,并且可以通过多种方式实现。例如,要使用 TypeScript 实现您的要求,您可以执行以下操作:
/* Define a type describing the structure of your desired object (optional)
*/
type CustomType = { [key: number]: Array<any> };
/* Create your object based on type definition with numbers as keys and array
of objects as values */
const yourObject: CustomType = {
1 : [1,2,3],
2 : ['a','b','c'],
3 : [ new Object(), new Object() ]
};
在 JavaScript 中,同样可以通过省略输入来实现:
const yourObject = {
1: [1, 2, 3],
2: ['a', 'b', 'c'],
3: [new Object(), new Object()]
};
console.log(yourObject);
/* Adding more letters to value at key 2 */
yourObject[2].push('d', 'e', 'f');
console.log(yourObject);
添加回答
举报