简体   繁体   English

展平嵌套对象打字稿

[英]flattening nested objects typescript

I am looking to flatten a nested object in my controller (new to Loopback and Typescript)我希望在我的控制器中展平一个嵌套对象(Loopback 和 Typescript 的新手)

Here is my model :这是我的模型:

export class SampleModel {
  id: number;
  code: number;
  guide?: string;
  gradeData?: string;
}

Here is an example object :这是一个示例对象:

{
  "id": 1,
  "code": 12345,
  "guide": "Guide for 2021",
  "gradeData": {
    "en": "Eng grade",
    "de": "Ger grade"
  }
}

Here is my controller:这是我的控制器:

// returns an array of SampleModel objects
@get('/guides')
async find(
@param.query.string('lang') lang: string,
@param.filter(SampleModel) filter?: Filter<SampleModel>
): Promise<SampleModel[]> {
return this.sampleModelRepository.find(filter); //this returns Promise<SampleModel[]>
}

I want to tweak this response a little based on lang .我想根据lang稍微调整一下这个响应。 For ex: if lang = en I want the response to look like例如:如果lang = en我希望响应看起来像

[
  {
    "id": 1,
    "code": 12345,
    "guide": "Guide for 2021",
    "gradeData": "Eng grade"
  }
]

Something like this?像这样的东西? Ofcource you need to make the langcode dynamic Ofcource 你需要使 langcode 动态化

[{
  "id": 1,
  "code": 12345,
  "guide": "Guide for 2021",
  "gradeData": {
    "en": "Eng grade",
    "de": "Ger grade"
  }
}].map(e=>{
    e.gradeData = e.gradeData["en"];
    return e;
})

Returned object:返回对象:

[
    {
        "id": 1,
        "code": 12345,
        "guide": "Guide for 2021",
        "gradeData": "Eng grade"
    }
]

Thanks to @Firewizz I was able to do this.感谢@Firewizz,我能够做到这一点。 Here is my updated controller :这是我更新的控制器:

  // returns an array of SampleModel objects
  @get("/guides")
  async find(
    @param.query.string("lang") lang: string,
    @param.filter(SampleModel) filter?: Filter<SampleModel>
  ): Promise<SampleModel[]> {
    const res = this.sampleModelRepository.find(filter); //this returns Promise<SampleModel[]>
    if (lang != null) {
      (await res).map((e) => {
        if (e.gradeData != null && e.gradeData.hasOwnProperty(lang)) {
          e.gradeData = new Map(Object.entries(e.gradeData)).get(locale);
          // not sure why this is required but when I tried
          // `e.gradeData = e.gradeData[locale];`
          // I get compilation error " Element implicity has an 'any' type because index expression is not of type 'number' " maybe because gradeData is defined as a String but when I do
          // console.log(typeof e.gradeData)
          // I get object

          // I also tried
          // `e.gradeData = JSON.parse(e.gradeData)[locale];`
          // I get " SyntaxError: Unexpected token o in JSON at position 1 " and that could be because it's already an object

          // I then tried
          // `e.gradeData = JSON.parse(JSON.stringify(e.gradeData))[locale];`
          // this also works but I think converting this to a map as a workaround is better
        }
        return e;
      });
    }

    return res;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM