我有两个数组,旧的和新的,在每个位置保存对象。我将如何同步或找到增量(即新数组与旧数组相比新增,更新和删除的内容)
var o = [
{id:1, title:"title 1", type:"foo"},
{id:2, title:"title 2", type:"foo"},
{id:3, title:"title 3", type:"foo"}
];
var n = [
{id:1, title:"title 1", type:"foo"},
{id:2, title:"title updated", type:"foo"},
{id:4, title:"title 4", type:"foo"}
];
使用上述数据,使用id作为键,我们发现id = 2的项目有更新的标题,id = 3的项目被删除,id = 4的项目是新的。
是否存在具有有用功能的现有库,或者是循环和内循环的情况,比较每一行......例如。
for(var i=0, l=o.length; i<l; i++)
{
for(var x=0, ln=n.length; x<ln; x++)
{
//compare when o[i].id == n[x].id
}
}
进行三次这种比较,找到新的,更新的和删除的?
答案 0 :(得分:16)
做你需要的东西毫无魔力。您需要遍历两个对象以查找更改。一个很好的建议是将您的结构转换为地图以加快搜索速度。
/**
* Creates a map out of an array be choosing what property to key by
* @param {object[]} array Array that will be converted into a map
* @param {string} prop Name of property to key by
* @return {object} The mapped array. Example:
* mapFromArray([{a:1,b:2}, {a:3,b:4}], 'a')
* returns {1: {a:1,b:2}, 3: {a:3,b:4}}
*/
function mapFromArray(array, prop) {
var map = {};
for (var i=0; i < array.length; i++) {
map[ array[i][prop] ] = array[i];
}
return map;
}
function isEqual(a, b) {
return a.title === b.title && a.type === b.type;
}
/**
* @param {object[]} o old array of objects
* @param {object[]} n new array of objects
* @param {object} An object with changes
*/
function getDelta(o, n, comparator) {
var delta = {
added: [],
deleted: [],
changed: []
};
var mapO = mapFromArray(o, 'id');
var mapN = mapFromArray(n, 'id');
for (var id in mapO) {
if (!mapN.hasOwnProperty(id)) {
delta.deleted.push(mapO[id]);
} else if (!comparator(mapN[id], mapO[id])){
delta.changed.push(mapN[id]);
}
}
for (var id in mapN) {
if (!mapO.hasOwnProperty(id)) {
delta.added.push( mapN[id] )
}
}
return delta;
}
// Call it like
var delta = getDelta(o,n, isEqual);
请参阅http://jsfiddle.net/wjdZ6/1/以获取示例
答案 1 :(得分:1)
与@Juan Mendes
相同,但对于内置Maps而言,效率更高(查找附加值)
function mapDelta(oldMap, newMap, compare) {
var delta = {
added: [],
deleted: [],
changed: []
};
var newKeys = new Set(newMap.keys());
oldMap.forEach(function (oldValue, oldKey) {
newKeys.delete(oldKey);
var newValue = newMap.get(oldKey);
if (newValue == undefined) {
delta.deleted.push({ key: oldKey, value: oldValue });
return;
}
else if (!compare(oldValue, newValue)) {
delta.changed.push({ key: oldKey, oldValue: oldValue, newValue: newValue });
}
});
newKeys.forEach(function (newKey) {
delta.added.push({ key: newKey, value: newMap.get(newKey) });
});
return delta;
}
并使用typescript
function mapDelta<K, T>(oldMap: Map<K, T>, newMap: Map<K, T>, compare: (a: T, b: T) => boolean) {
const delta = {
added: [] as { key: K, value: T }[],
deleted: [] as { key: K, value: T }[],
changed: [] as { key: K, oldValue: T, newValue: T }[]
};
const newKeys = new Set(newMap.keys());
oldMap.forEach((oldValue, oldKey) => {
newKeys.delete(oldKey);
const newValue = newMap.get(oldKey);
if (newValue == undefined) {
delta.deleted.push({ key: oldKey, value: oldValue });
return;
} else if (!compare(oldValue, newValue)) {
delta.changed.push({ key: oldKey, oldValue: oldValue, newValue: newValue });
}
})
newKeys.forEach((newKey) => {
delta.added.push({ key: newKey, value: newMap.get(newKey) });
})
return delta;
}
答案 2 :(得分:0)
这是@Juan Mendes
回答的打字稿版本
mapFromArray(array: Array<any>, prop: string): { [index: number]: any } {
const map = {};
for (let i = 0; i < array.length; i++) {
map[array[i][prop]] = array[i];
}
return map;
}
isEqual(a, b): boolean {
return a.title === b.title && a.type === b.type;
}
getDelta(o: Array<any>, n: Array<any>, comparator: (a, b) => boolean): { added: Array<any>, deleted: Array<any>, changed: Array<any> } {
const delta = {
added: [],
deleted: [],
changed: []
};
const mapO = this.mapFromArray(o, 'id');
const mapN = this.mapFromArray(n, 'id');
for (const id in mapO) {
if (!mapN.hasOwnProperty(id)) {
delta.deleted.push(mapO[id]);
} else if (!comparator(mapN[id], mapO[id])) {
delta.changed.push(mapN[id]);
}
}
for (const id in mapN) {
if (!mapO.hasOwnProperty(id)) {
delta.added.push(mapN[id]);
}
}
return delta;
}