setValue动态到aggular7中的对象

发布于 2025-01-27 06:45:15 字数 574 浏览 1 评论 0原文

我有这个代码,我想重构以更好的解决方案:

    async onServerRequestForDraft(res: { value: {}; tabType: ETabType; }) {
        res.value = this.uiForm.value;
       
        res.value = this.data;
       res.value['brokerage'] = this.uiForm.value.brokerage != null ? this.uiForm.value.brokerage : { id: 0, name: null };
        res.value['org'] = this.uiForm.value.org; 
        res.value['selectedPhone'] = this.uiForm.value.selectedPhone || '';
        res.value['customerName'] = this.uiForm.value.customerName || '';
}

我无法使用foreach,因为RES不是数组。也许是另一个解决方案?

I have this code which I want to refactor to better solution:

    async onServerRequestForDraft(res: { value: {}; tabType: ETabType; }) {
        res.value = this.uiForm.value;
       
        res.value = this.data;
       res.value['brokerage'] = this.uiForm.value.brokerage != null ? this.uiForm.value.brokerage : { id: 0, name: null };
        res.value['org'] = this.uiForm.value.org; 
        res.value['selectedPhone'] = this.uiForm.value.selectedPhone || '';
        res.value['customerName'] = this.uiForm.value.customerName || '';
}

I can't use forEach because res is not an array. maybe another solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2025-02-03 06:45:15

尽管可以使用它,但您不需要工作。

   async onServerRequestForDraft(res: { value: {}, tabType: ETabType }) {
      Object.assign(res.value, {
          'brokerage': this.uiForm.value.brokerage ?? { id: 0, name: null },
          'org': this.uiForm.value.selectedPhone ?? '',
          'customerName': this.uiForm.value.customerName ?? '';
      })

      return res;
}

如果您只想使用循环,则可以这样做:

async onServerRequestForDraft(res: { value: {}, tabType: ETabType }) {

     for (const key in this.uiForm.value) {
         if (key === 'brokerage') {
          Object.assign(res.value, {key : this.uiForm.value[key] ?? { id: 0, name: null }});
          continue;
         }
         Object.assign(res.value, {key : this.uiForm.value[key] ?? ''});
      }
      

      return res;
}

You don't need forEach for it to work though you can use it.

   async onServerRequestForDraft(res: { value: {}, tabType: ETabType }) {
      Object.assign(res.value, {
          'brokerage': this.uiForm.value.brokerage ?? { id: 0, name: null },
          'org': this.uiForm.value.selectedPhone ?? '',
          'customerName': this.uiForm.value.customerName ?? '';
      })

      return res;
}

If you want to use loop only, you can do it this way:

async onServerRequestForDraft(res: { value: {}, tabType: ETabType }) {

     for (const key in this.uiForm.value) {
         if (key === 'brokerage') {
          Object.assign(res.value, {key : this.uiForm.value[key] ?? { id: 0, name: null }});
          continue;
         }
         Object.assign(res.value, {key : this.uiForm.value[key] ?? ''});
      }
      

      return res;
}
向地狱狂奔 2025-02-03 06:45:15

您可以在这样的对象上使用foreach:

Object.entries(this.uiForm.value).forEach((item) => {
    // item[0]  object key
    // item[1]  object value
 });

u can use forEach on Object like this :

Object.entries(this.uiForm.value).forEach((item) => {
    // item[0]  object key
    // item[1]  object value
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文