HarmonyOS object怎么转成Record?

如题:HarmonyOS object怎么转成Record?

阅读 465
1 个回答

参考示例:

interface testObject {
  bundleName: string
  action: string
  entities: number
}

@Entry
@Component
struct Index {
  @State testObj:testObject={
    bundleName: 'hhh',
    action: 'eee',
    entities: 10
  }

  @State testRecord:Record<string,string|number>={}

  aboutToAppear(): void {
    const arr = Object.keys(this.testObj);
    const arr1:string[]|number[] = Object.values(this.testObj);
    for (let i = 0; i < arr.length; i++) {
    this.testRecord[arr[i]]=arr1[i]
  }
  console.log(JSON.stringify(this.testRecord) );
  }

  build() {
    Column(){
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏