如何从arrayList中提取对象并将其添加到另一个arrayList中

发布于 2025-01-11 13:32:40 字数 1022 浏览 0 评论 0原文

我正在尝试通过修改内部对象之一将一个数组列表转换为另一个数组列表

   let array1 = [
            {
              name: "test",
              address: "testaddress",
              state: {
                2022: {
                  January: { month_state: "pending" },
                },
              },
            },
            {
              name: "test2",
              address: "testaddress2",
              state: {
                2022: {
                  January: { month_state: "pending" },
                },
              },
            },
          ];

,我想转换成这个,这是正确的方法吗?谢谢。

let array2 = [
            {
              name: "test",
              address: "testaddress",
              2022: {
                January: { month_state: "pending" },
              },
            },

            {
              name: "test2",
              address: "testaddress2",
              2022: {
                January: { month_state: "pending" },
              },
            },
          ];

I am trying to convert one arraylist to another by modifying one of the objects inside

   let array1 = [
            {
              name: "test",
              address: "testaddress",
              state: {
                2022: {
                  January: { month_state: "pending" },
                },
              },
            },
            {
              name: "test2",
              address: "testaddress2",
              state: {
                2022: {
                  January: { month_state: "pending" },
                },
              },
            },
          ];

And I want to convert into this, which is the correct way? Thanks.

let array2 = [
            {
              name: "test",
              address: "testaddress",
              2022: {
                January: { month_state: "pending" },
              },
            },

            {
              name: "test2",
              address: "testaddress2",
              2022: {
                January: { month_state: "pending" },
              },
            },
          ];

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

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

发布评论

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

评论(2

决绝 2025-01-18 13:32:40
let array2 = array1.map(({ state, ...rest }) => ({ ...rest, ...state }))

这段代码就可以了。

let array2 = array1.map(({ state, ...rest }) => ({ ...rest, ...state }))

This code will do.

遇见了你 2025-01-18 13:32:40

有多种方法可以实现您的要求。

我将向您展示使用 .map 的函数方法。

let array1 = [
    {
        name: "test",
        address: "testaddress",
        state: {
            2022: {
                January: { month_state: "pending" }
            }
        }
    },
    {
        name: "test2",
        address: "testaddress2",
        state: {
            2022: {
                January: { month_state: "pending" }
            }
        }
    }
];

let array2 = array1.map((el) => {
    const tmp = {
        name: el.name,
        adress: el.adress
    };
    
    // Here we iterate el.state, so we can extract each year.
    /* We need to dynamically generate a property name (each year). 
So we use a feature called "computed property names (ES2015)" */
    for (const prop in el.state) {
        tmp[prop] = el.state[prop];
    }

    return tmp;
});
console.log(array2);

您还可以使用 for 循环,这也是一种有效的方法。

There are several ways to achieve what you're asking.

I'll show you using the functional approach with .map

let array1 = [
    {
        name: "test",
        address: "testaddress",
        state: {
            2022: {
                January: { month_state: "pending" }
            }
        }
    },
    {
        name: "test2",
        address: "testaddress2",
        state: {
            2022: {
                January: { month_state: "pending" }
            }
        }
    }
];

let array2 = array1.map((el) => {
    const tmp = {
        name: el.name,
        adress: el.adress
    };
    
    // Here we iterate el.state, so we can extract each year.
    /* We need to dynamically generate a property name (each year). 
So we use a feature called "computed property names (ES2015)" */
    for (const prop in el.state) {
        tmp[prop] = el.state[prop];
    }

    return tmp;
});
console.log(array2);

You could also use a for loop, wich is also a valid approach.

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