[英]How to merge objects from two arrays
我有兩個 arrays 對象
let current = [
{
categoryId: 18,
id: 'fire_18',
test: 'test'
}, {
oldItem: "items that dont exist in new array should be deleted"
}
]
let new_data = [
{
id: 'fire_18',
categoryId: 18,
test1: 'test1',
}, {
newItem: "new items should be added to final"
}
]
我需要合並它們,這樣結果就不會丟失current
數組中的數據。 期望的結果看起來像這樣
[
{
id: 'fire_18',
categoryId: 18,
test1: 'test1',
test: 'test'
}, {
newItem: "new items should be added to final"
}
]
這是我的嘗試
let current = [ { categoryId: 18, id: 'fire_18', test: 'test' }, { oldItem: "items that dont exist in new array should be deleted" } ] let new_data = [ { id: 'fire_18', categoryId: 18, test1: 'test1', }, { newItem: "new items should be added to final" } ] console.log(' === Current ==>', current); console.log(' === New ==>', new_data); new_data.map(newItem => { let currentMatch; try { // incase new item newItem.categoryId wont exist currentMatch = current.find(c => c.categoryId == newItem.categoryId) || {}; } catch (error) { // in that case, no need to merge currentMatch = {}; } return Object.assign(currentMatch, newItem); }); console.log(' === Merged ==>', new_data);
主要的錯誤仍然是合並后缺少current
arr 的test
密鑰。
任何人都可以修改筆以不從current
arr 中刪除鍵嗎?
請注意, Array.map
方法返回一個新數組。 您沒有記錄這個合並的新陣列 - 而是您的舊 arrays 之一。
此外,您不需要在這里try catch
。 您可能還注意到,如果categoryId
未定義,對象仍將匹配。 所以,你需要檢查一下。
let current = [ { categoryId: 18, id: 'fire_18', test: 'test' }, { oldItem: "items that dont exist in new array should be deleted" } ] let new_data = [ { id: 'fire_18', categoryId: 18, test1: 'test1', }, { newItem: "new items should be added to final" } ] newArray = new_data.map(newItem => { let currentMatch; currentMatch = current.find(c => c.categoryId === newItem.categoryId && newItem.categoryId) || {}; return Object.assign(currentMatch, newItem); }); console.log(' === Merged ==>', newArray);
您可以為此使用 lodash。
finalArr = _(_.flatten([current, new_data]))
.groupBy('id')
.map(_.spread(_.assign))
.value();
像這樣? 我不確定您期望newItem
和oldItem
條目會發生什么。
let current = [{ categoryId: 18, id: 'fire_18', test: 'test' }] let new_data = [{ id: 'fire_18', categoryId: 18, test1: 'test1', }] const result = Object.values([...current, ...new_data].reduce((acc, { categoryId, ...other }) => ({...acc, [categoryId]: { categoryId, ...other } }), {})); console.log(result);
您可以使用 es6 擴展運算符來滿足您的要求
var current = [{
categoryId: 18,
id: 'fire_18',
name: 'Servers',
slugName: 'fire_18',
test: 'test'
}]
var new_data = [{
id: 'fire_18',
categoryId: 18,
name: 'Servers',
slugName: 'fire_18'
}]
const results = new_data.map(item => {
let newItem = { ...item };
const currentMatchItem = current.find(c => c.categoryId == newItem.categoryId);
if (currentMatchItem) {
newItem = { ...currentMatchItem, ...newItem };
}
return newItem;
});
console.log(results);
我找到了這個。 在你的代碼上是這樣的:
let current = [ { categoryId: 18, id: 'fire_18', test: 'test' }, { oldItem: "items that dont exist in new array should be deleted" } ] let new_data = [ { id: 'fire_18', categoryId: 18, test1: 'test1', }, { newItem: "new items should be added to final" } ] function merge(a, b, prop){ var reduced = a.filter(function(aitem){ return. b;find(function(bitem){ return aitem[prop] === bitem[prop]; }); }). return reduced;concat(b). } console,log(merge(current, new_data; "id") );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.