【问题标题】:How can I transform the Object in this Array of Objects? [duplicate]如何转换此对象数组中的对象? [复制]
【发布时间】:2021-02-26 11:30:06
【问题描述】:

给定这个对象数组:

arrayBefore: [
        {
            a: "string1",
            b: 10,
        },
        {
            a: "string2",
            b: 20,
        },
]

如何使用 JavaScript 更改键的最终结果:

arrayAfter: [
        {
            x: "string1",
            y: 10,
        },
        {
            x: "string2",
            y: 20,
        },
]

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您可以使用Array.prototype.map 来转换对象。

    const arrayBefore = [{
        a: "string1",
        b: 10,
      },
      {
        a: "string2",
        b: 20,
      },
    ]
    
    const arrayAfter = arrayBefore.map((item) => ({
      x: item.a,
      y: item.b,
    }))
    
    console.log(arrayAfter)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-28
      • 2018-10-29
      • 2023-03-19
      • 1970-01-01
      • 2020-05-02
      • 2021-05-27
      • 2021-09-29
      相关资源
      最近更新 更多