如何在角度以不同格式构造对象

发布于 2025-02-11 08:14:03 字数 1017 浏览 1 评论 0原文

我是新来的角。我正在尝试重组我从API响应中获得的对象。我需要将当前对象构造为所需的对象。

这是下面的当前对象

let temp = {
"imagesList": {
    "de": [{
       "path": "",
       "text": "desc text"
     },
     {
       "path": "",
       "text": "desc text"
     }],
     "en": [{
       "path": "",
       "text": "desc text"
     }]
   }
}

是所需的对象,从上面定义的当前对象

let temp = [
    {
      "path": "",
      "description": [
        {
          "language": "en",
          "text": "desc text"
        }
      ]
    },
    {
      "path": "",
      "description": [
        {
          "language": "de",
          "text": "desc text"
        }
      ]
    },
    {
      "path": "",
      "description": [
        {
          "language": "de",
          "text": "desc text"
        }
      ]
    }, 
 
  ]

如何使用Angular中的Typescript实现此目标。 沙盒 link 提前致谢

I'm new to angular. I'm trying to restructure the object which I'm getting from the API response. I need to structure the current object to desired object.

This is the Current Object

let temp = {
"imagesList": {
    "de": [{
       "path": "",
       "text": "desc text"
     },
     {
       "path": "",
       "text": "desc text"
     }],
     "en": [{
       "path": "",
       "text": "desc text"
     }]
   }
}

Below is the Desired Object which is needed from the Current Object defined above

let temp = [
    {
      "path": "",
      "description": [
        {
          "language": "en",
          "text": "desc text"
        }
      ]
    },
    {
      "path": "",
      "description": [
        {
          "language": "de",
          "text": "desc text"
        }
      ]
    },
    {
      "path": "",
      "description": [
        {
          "language": "de",
          "text": "desc text"
        }
      ]
    }, 
 
  ]

How to achieve this using typescript in angular.
Sand box Link
Thanks in advance

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

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

发布评论

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

评论(1

帅气称霸 2025-02-18 08:14:03

在这里,您有解决方案:

 ngOnInit() {
    console.log(this.temp);
    const transformed: any[] =[]; 

    for (const key in this.temp.imagesList) {
      if (this.temp.imagesList.hasOwnProperty(key)) {
          for(const o of this.temp.imagesList[key]){
            transformed.push({
              path:o.path,
              description:[{
                language:key,
                text:o.text
              }]
            });
          }
      }
    }
    console.log(transformed);
  }

顺便说一句:您将使用JavaScript完全相同。这并不是真正的角度或打字稿问题;

here you have the solution:

 ngOnInit() {
    console.log(this.temp);
    const transformed: any[] =[]; 

    for (const key in this.temp.imagesList) {
      if (this.temp.imagesList.hasOwnProperty(key)) {
          for(const o of this.temp.imagesList[key]){
            transformed.push({
              path:o.path,
              description:[{
                language:key,
                text:o.text
              }]
            });
          }
      }
    }
    console.log(transformed);
  }

BTW: you would do it exactly the same with javascript. It's not really an angular or typescript question;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文