gpt4 book ai didi

javascript - 将 JS 对象数组推送到另一个具有匹配对象键的对象数组

转载 作者:行者123 更新时间:2023-11-30 11:12:17 25 4
gpt4 key购买 nike

我有一堆来自多个来源的对象,我想将它们合并,这样我就可以处理一个大对象,而不是说 5 或 10 个对象。

假设我的主要对象数组包含我想将其他数组推送到的员工基本数据是

var employees = [
{emp_id: 1, emp_name: "John D", phone:"123456"},
{emp_id: 2, emp_name: "Mary J", phone:"234567"},
{emp_id: 3, emp_name: "Doe J", phone:"345678"},
{emp_id: 4, emp_name: "Jane M", phone:"456789"}
]

还有另一个包含员工工作历史的对象数组:

var employee_history = [
{emp_id: 1, company: "ABC", Years: 4},
{emp_id: 2, company: "BCD", Years: 3},
{emp_id: 3, company: "CDE", Years: 2},
{emp_id: 4, company: "DEF", Years: 1}
]

最终的对象数组包含员工的居住历史:

var cities_lived = [
{emp_id: 1, city: "Moscow", Years: 1},
{emp_id: 1, city: "Doha", Years: 1},
{emp_id: 2, city: "Cairo", Years: 2},
{emp_id: 2, city: "London", Years: 1},
{emp_id: 3, city: "Tunis", Years: 2},
{emp_id: 3, city: "Beijing", Years: 2},
{emp_id: 4, city: "New York", Years: 1},
{emp_id: 4, city: "Capetown", Years: 1}
]

所以我想使用 emp_idemployee_historycities_lived 推送到 employees 内的各个对象中属性匹配并具有这样的输出或类似的东西,只要它在单个对象内:

[
{
emp_id: 1,
emp_name: "John D",
phone: "123456",
company: "ABC",
Years: 4,
cities: [
{emp_id: 1, city: "Doha", Years: "1"},
{emp_id: 1, city: "Doha", Years: "1"}
]
},
{},
{},
...
]

我怎样才能做到这一点?

我的麻烦解决方案是循环每个对象数组并创建我将数据推送到的新对象,然后最后将结果推送到主对象数组中。但我不喜欢必须手动完成所有这些工作的想法,即便如此我也不确定如何将结果推送到 emp_id 属性匹配的主对象数组中。

最佳答案

您可以尝试编写几乎不做任何事情的小函数,并将它们组合成一个将项目合并为一个项目的函数。

然后将所有需要合并的数据与他们的合并功能合并,并将员工减少到合并所有内容的新值。

const employees = [{"emp_id":1,"emp_name":"John D","phone":"123456"},{"emp_id":2,"emp_name":"Mary J","phone":"234567"},{"emp_id":3,"emp_name":"Doe J","phone":"345678"},{"emp_id":4,"emp_name":"Jane M","phone":"456789"}];
const employee_history = [{"emp_id":1,"company":"ABC","Years":4},{"emp_id":2,"company":"BCD","Years":3},{"emp_id":3,"company":"CDE","Years":2},{"emp_id":4,"company":"DEF","Years":1}];
const cities_lived = [{"emp_id":1,"city":"Moscow","Years":1},{"emp_id":1,"city":"Doha","Years":1},{"emp_id":2,"city":"Cairo","Years":2},{"emp_id":2,"city":"London","Years":1},{"emp_id":3,"city":"Tunis","Years":2},{"emp_id":3,"city":"Beijing","Years":2},{"emp_id":4,"city":"New York","Years":1},{"emp_id":4,"city":"Capetown","Years":1}];
const whatever = [{ emp_id: 1, whatever: 'whatever' }];//extra to merge item

//merges items with the merger function from toMerge array in uniqueArray
// if they match using matcher
const mergeIn = (uniqueArray, toMerge, matcher, merger) =>
uniqueArray.map((item) =>
merger(item, toMerge.filter(matcher(item))),
);
//create a merger function set item[itemFieldName] with a mapped result
// of others using mapper function
const merger = (itemFieldName, mapper) => (
item,
others,
) => ({
...item,
[itemFieldName]: others.map(mapper),
});
//match on emp_id
const matchEpmId = (item) => (other) =>
item.emp_id === other.emp_id;

console.log(
[
[
cities_lived,
//merger function that sets item.cities with others mapped to {city,Years}
merger('cities', ({ city, Years }) => ({ city, Years}))
],
[
employee_history,
//merger function that sets item.history with others mapped to {company,Years}
merger('history', ({ company, Years }) => ({ company, Years}))
],
[
whatever,//extra to merge items
merger('whatever', ({ whatever }) => whatever),
],
].reduce(
(result, [other, merger]) =>
mergeIn(result, other, matchEpmId, merger),
employees,
),
);

关于javascript - 将 JS 对象数组推送到另一个具有匹配对象键的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53208344/

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