做一个" DIFF"就在JavaScript / jQuery的关联数组? [英] Doing a "Diff" on an Associative Array in javascript / jQuery?
本文介绍了做一个" DIFF"就在JavaScript / jQuery的关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有两个关联数组,这将是做差异对他们的价值观的最有效方法是什么?
If I have two associative arrays, what would be the most efficient way of doing a diff against their values?
例如,给出:
array1 = {
foreground: 'red',
shape: 'circle',
background: 'yellow'
};
array2 = {
foreground: 'red',
shape: 'square',
angle: '90',
background: 'yellow'
};
我怎么会选一对另一方,使得项目的丢失的或的其他的是结果数组。在这种情况下,如果我想在数组2比较ARRAY1,它将返回:
How would I check one against the other, such that the items missing or additional are the resulting array. In this case, if I wanted to compare array1 within array2, it would return:
array3 = {shape: 'circle'}
虽然如果我ARRAY1内比较数组2,它将返回:
Whilst if I compared array2 within array1, it would return:
array3 = {shape: 'square', angle: '90'}
在此先感谢您的帮助!
Thanks in advance for your help!
推荐答案
试试这个:
function diff(obj1, obj2) {
var result = {};
$.each(obj1, function (key, value) {
if (!obj2.hasOwnProperty(key) || obj2[key] !== obj1[key]) {
result[key] = value;
}
});
return result;
}
这篇关于做一个" DIFF"就在JavaScript / jQuery的关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文