gpt4 book ai didi

javascript - 比较两个数组对象数据并作为一个对象合并到 javascript 中的数组中?

转载 作者:行者123 更新时间:2023-11-30 09:20:00 28 4
gpt4 key购买 nike

我的数组:

const create = [{"month":"Jan","createCount":"4"},{"month":"Feb","createCount":"5"}];

const close = [{"month":"Jan","closeCount":"3"},{"month":"Feb","closeCount":"5"}];

我需要将数组对象合并到比较月份字段。

我想要这样的结果...

const data = [{"month":"Jan","createCount":"4","closeCount":"3"},{"month":"Feb","createCount":"5","closeCount":"5"}];

试过这段代码:

const data = [...create, ...close];

但这只是合并两个数组数据。

请给我任何解决方案!

最佳答案

您可以使用 .map()Object.assign() 方法:

const create = [{"month":"Jan","createCount":"4"}, {"month":"Feb","createCount":"5"}],
close = [{"month":"Jan","closeCount":"3"}, {"month":"Feb","closeCount":"5"}];

const result = create.map(
o => Object.assign({}, o, close.find(({month}) => month === o.month))
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 比较两个数组对象数据并作为一个对象合并到 javascript 中的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52362133/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com