Ramda.js 合并两个共享相同 属性 ID 的对象数组
Ramda.js Combine two array of objects that share the same property ID
我有这两个对象数组
todos: [
{
id: 1,
name: 'customerReport',
label: 'Report send to customer'
},
{
id: 2,
name: 'handover',
label: 'Handover (in CRM)'
},
]
并且:
todosMoreDetails: [
{
id: 1,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: []
},
{
id: 2,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: []
}
]
因此最终的对象数组将是两者的组合,基于对象 ID,如下所示:
FinalTodos: [
{
id: 1,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: [],
name: 'customerReport',
label: 'Report send to customer'
},
{
id: 2,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: [],
name: 'handover',
label: 'Handover (in CRM)'
}
]
我试过 merge
mergeAll
和 mergeWithKey
但我可能遗漏了一些东西
我猜合并只用于数组。搜索对象 "extend"。也许将待办事项的详细信息不存储在单独的对象中是更好的解决方案。
- 使用jQuery? https://api.jquery.com/jquery.extend/
- 使用下划线? http://underscorejs.org/#extend
- 本地方法? https://gomakethings.com/vanilla-javascript-version-of-jquery-extend/
使用下划线:
var result = [];
var entry = {};
_.each(todos, function(todo) {
_.each(todosMoreDetails, function(detail) {
if (todo.id == detail.id) {
entry = _.extend(todo, detail);
result.push(entry);
}
}
});
return result;
您可以使用中间 groupBy 实现此目的:
使用 groupBy:
将 todosMoreDetails 数组转换为由 todo 属性 ID 键控的对象
var moreDetailsById = R.groupBy(R.prop('id'), todosMoreDetails);
moreDetailsById是一个对象,key为id,value为todos数组。如果 id 是唯一的,这将是一个单例数组:
{
1: [{
id: 1,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: []
}]
}
现在通过将每个待办事项合并到您从分组视图中检索到的详细信息来转换待办事项数组:
var finalTodos = R.map(todo => R.merge(todo, moreDetailsById[todo.id][0]), todos);
另一种更详细的方法:
function mergeTodo(todo) {
var details = moreDetailsById[todo.id][0]; // this is not null safe
var finalTodo = R.merge(todo, details);
return finalTodo;
}
var moreDetailsById = R.groupBy(R.prop('id'), todosMoreDetails);
var finalTodos = todos.map(mergeTodo);
我有这两个对象数组
todos: [
{
id: 1,
name: 'customerReport',
label: 'Report send to customer'
},
{
id: 2,
name: 'handover',
label: 'Handover (in CRM)'
},
]
并且:
todosMoreDetails: [
{
id: 1,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: []
},
{
id: 2,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: []
}
]
因此最终的对象数组将是两者的组合,基于对象 ID,如下所示:
FinalTodos: [
{
id: 1,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: [],
name: 'customerReport',
label: 'Report send to customer'
},
{
id: 2,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: [],
name: 'handover',
label: 'Handover (in CRM)'
}
]
我试过 merge
mergeAll
和 mergeWithKey
但我可能遗漏了一些东西
我猜合并只用于数组。搜索对象 "extend"。也许将待办事项的详细信息不存储在单独的对象中是更好的解决方案。
- 使用jQuery? https://api.jquery.com/jquery.extend/
- 使用下划线? http://underscorejs.org/#extend
- 本地方法? https://gomakethings.com/vanilla-javascript-version-of-jquery-extend/
使用下划线:
var result = [];
var entry = {};
_.each(todos, function(todo) {
_.each(todosMoreDetails, function(detail) {
if (todo.id == detail.id) {
entry = _.extend(todo, detail);
result.push(entry);
}
}
});
return result;
您可以使用中间 groupBy 实现此目的:
使用 groupBy:
将 todosMoreDetails 数组转换为由 todo 属性 ID 键控的对象var moreDetailsById = R.groupBy(R.prop('id'), todosMoreDetails);
moreDetailsById是一个对象,key为id,value为todos数组。如果 id 是唯一的,这将是一个单例数组:
{
1: [{
id: 1,
checked: false,
link: {
type: 'url',
content: 'http://something.com'
},
notes: []
}]
}
现在通过将每个待办事项合并到您从分组视图中检索到的详细信息来转换待办事项数组:
var finalTodos = R.map(todo => R.merge(todo, moreDetailsById[todo.id][0]), todos);
另一种更详细的方法:
function mergeTodo(todo) {
var details = moreDetailsById[todo.id][0]; // this is not null safe
var finalTodo = R.merge(todo, details);
return finalTodo;
}
var moreDetailsById = R.groupBy(R.prop('id'), todosMoreDetails);
var finalTodos = todos.map(mergeTodo);