如何将一个打字稿 class 对象翻译成另一个,其中只有值发生变化而键保持不变?

How to translate one typescript class object to another, where only values change and keys are left unchanged?

我有两个classes/interfaces:

class ImageObjectsJson {
    [markerId : string] : WktGeometryJson[];
}

class ImageObjects {
    [markerId : string] : ImageObject[]
}

我知道 ImageObject 的构造函数,它以 WktGeometryJson 对象作为参数。所以我可以打电话:

const wkt : WktGeometryJson = {... something useful here ...};
const imageObject : ImageObject = new ImageObject(wkt);

如何创建 ImageObjects,具有 ImageObjectsJson,保持键不变?

工作示例 - 在 map function for objects (instead of arrays)

之上
interface WktGeometryJson {
  name: string
}

class ImageObject {
  imageName: string;

  constructor(json: WktGeometryJson) {
    this.imageName = json.name;
  }
}

class ImageObjectsJson {
    [markerId : string] : WktGeometryJson[];
}

class ImageObjects {
    [markerId : string] : ImageObject[]
}

const imJson: ImageObjectsJson= {
  aaa: [{name: '1'}, {name: '2'}],
  bbb: [{name: '3'}]
}

const imObj = Object.fromEntries(
  Object.entries(imJson).map(
    ([propName, wktArray]) => [propName, wktArray.map(wkt => new ImageObject(wkt))]
  )
);

console.log(imObj);

Playground link