Spaces:
Sleeping
Sleeping
File size: 676 Bytes
d605f27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class DictArray extends Array {
constructor (jsonObj?: object) {
super();
if (jsonObj) {
console.assert(typeof jsonObj === "object", "invalid input type:", jsonObj);
if (Array.isArray(jsonObj))
Object.keys(jsonObj).forEach(index => this[index] = jsonObj[index]);
else
Object.entries(jsonObj).forEach(([key, value]) => this[key] = value);
}
}
toJSON () {
return Object.keys(this).reduce((dict, index) => (dict[index] = this[index], dict), {__prototype: "DictArray"});
}
clear () {
Object.keys(this).forEach(index => delete this[index]);
this.length = 0;
}
clone () {
return new DictArray(this);
}
};
export default DictArray;
|