比较两个JavaScript对象的键
我有两组JavaScript对象。我想比较object1和object2,然后得到object1中但不在object2中的所有键的列表。我一直在寻找帮助我的资源,但最终我只找到了简单对象的比较函数。我想比较的对象有很多嵌套。我在底部包括了一个例子 我如何制作一个函数来比较这两个对象?是否有可能创建一个灵活的函数,如果对象发生变化并包含更多嵌套,该函数也会起作用比较两个JavaScript对象的键,javascript,json,compare,Javascript,Json,Compare,我有两组JavaScript对象。我想比较object1和object2,然后得到object1中但不在object2中的所有键的列表。我一直在寻找帮助我的资源,但最终我只找到了简单对象的比较函数。我想比较的对象有很多嵌套。我在底部包括了一个例子 我如何制作一个函数来比较这两个对象?是否有可能创建一个灵活的函数,如果对象发生变化并包含更多嵌套,该函数也会起作用 const object1 = { "gender": "man", "age": 33, "origin":
const object1 = {
"gender": "man",
"age": 33,
"origin": "USA",
"jobinfo": {
"type": "teacher",
"school": "Wisconsin"
},
"children": [
{
"name": "Daniel",
"age": 12,
"pets": [
{
"type": "cat",
"name": "Willy",
"age": 2
},
{
"type": "dog",
"name": "jimmie",
"age": 5
}
]
},
{
"name": "Martin",
"age": 14,
"pets": [
{
"type": "bird",
"name": "wagner",
"age": 12
}
]
}
],
"hobbies": {
"type": "football",
"sponsor": {
"name": "Pepsi",
"sponsorAmount": 1000,
"contact": {
"name": "Leon",
"age": 59,
"children": [
{
"name": "James",
"pets": [
{
"type": "dog",
"age": 4
}
]
}
]
}
}
}
}
const object2 = {
"gender": "man",
"jobinfo": {
"type": "teacher"
},
"children": [
{
"name": "Daniel",
"age": 12,
"pets": [
{
"type": "cat",
"name": "Willy",
"age": 2
},
{
"type": "dog",
"name": "jimmie",
"age": 5
}
]
}
]
}
所以,通过比较这两个对象,我想要实现的是,在本例中,有一个数组返回,它由object1中的键组成,而不是object2中的键。所以这个数组看起来像这样
["age", "hobbies", "type", "sponsor", "name", "sponsorAmount", "contact", "name", "age", "children", "name", "pets", "type", "age"].
这就是我到目前为止所做的。这是一种工作方式。但它并没有打印出年龄,例如,因为年龄是存在于多个嵌套对象中的属性。
jsfiddle:
我查看了以下资源:
getAllKeyNames()
递归函数从指定对象获取所有唯一的键名:
var object1={“性别”:“男人”、“年龄”:33,“出身”:“美国”、“工作信息”:{“类型”:“教师”、“学校”:“威斯康星州”},“儿童”:[{“姓名”:“丹尼尔”、“年龄”:12,“宠物”:[{“类型”:“猫”、“姓名”:“威利”、“年龄”:2},{“类型”:“狗”、“姓名”:“吉米”、“年龄”:5}}、{“姓名”:“马丁”、“年龄”:14,“宠物”:[{“类型”:“鸟”、“姓名”:“瓦格纳”、“年龄”:12}],“爱好”:“足球”:,“赞助商”:{“姓名”:“百事可乐”,“赞助商金额”:1000,“联系人”:{“姓名”:“里昂”,“年龄”:59岁,“儿童”:[{“姓名”:“詹姆斯”,“宠物”:[{“类型”:“狗”,“年龄”:4}]}}}},
对象2={“性别”:“男人”、“年龄”:33,“出身”:“美国”、“工作信息”:{“类型”:“教师”、“学校”:“威斯康星州”},“儿童”:[{“姓名”:“丹尼尔”、“年龄”:12,“宠物”:[{“类型”:“猫”、“姓名”:“威利”、“年龄”:2},{“类型”:“狗”、“姓名”:“吉米”、“年龄”:5};
函数getAllKeyNames(o,res){
Object.keys(o).forEach(函数(k){
if(Object.prototype.toString.call(o[k])==“Object Object]”){
getAllKeyNames(o[k],res);
}else if(Array.isArray(o[k])){
o[k].forEach(函数(v){
getAllKeyNames(v,res);
});
}
决议增补(k);
});
}
变量o1Keys=new Set(),o2Keys=new Set();
getAllKeyNames(object1,o1Keys);//object1的唯一键
getAllKeyNames(object2,o2Keys);//object2的唯一键
//获取object1中但不在object2中的所有键的列表
var diff=[…o1Keys].过滤器((x)=>!o2Keys.has(x));
控制台日志(diff);
复杂解决方案使用设置对象和自定义getAllKeyNames()
递归函数从指定对象获取所有唯一的键名:
var object1={“性别”:“男人”、“年龄”:33,“出身”:“美国”、“工作信息”:{“类型”:“老师”、“学校”:“威斯康星州”},“儿童”:[{“姓名”:“丹尼尔”、“年龄”:12,“宠物”:[{“类型”:“猫”、“姓名”:“威利”、“年龄”:2},{“类型”:“狗”、“姓名”:“吉米”、“年龄”:5}}、{“姓名”:“马丁”、“年龄”:14,“宠物”:[{“类型”:“鸟”、“姓名”:“瓦格纳”、“年龄”:12}],“爱好”:“{“类型”:赞助商:{“姓名”:“百事可乐”,“赞助商金额”:1000,“联系人”:{“姓名”:“里昂”,“年龄”:59,“儿童”:[{“姓名”:“詹姆斯”,“宠物”:[{“类型”:“狗”,“年龄”:4}]}}}},
对象2={“性别”:“男人”、“年龄”:33,“出身”:“美国”、“工作信息”:{“类型”:“教师”、“学校”:“威斯康星州”},“儿童”:[{“姓名”:“丹尼尔”、“年龄”:12,“宠物”:[{“类型”:“猫”、“姓名”:“威利”、“年龄”:2},{“类型”:“狗”、“姓名”:“吉米”、“年龄”:5};
函数getAllKeyNames(o,res){
Object.keys(o).forEach(函数(k){
if(Object.prototype.toString.call(o[k])==“Object Object]”){
getAllKeyNames(o[k],res);
}else if(Array.isArray(o[k])){
o[k].forEach(函数(v){
getAllKeyNames(v,res);
});
}
决议增补(k);
});
}
变量o1Keys=new Set(),o2Keys=new Set();
getAllKeyNames(object1,o1Keys);//object1的唯一键
getAllKeyNames(object2,o2Keys);//object2的唯一键
//获取object1中但不在object2中的所有键的列表
var diff=[…o1Keys].过滤器((x)=>!o2Keys.has(x));
控制台日志(diff);
您可以使用对象进行计数
函数getCount(对象,键,inc){
Object.keys(对象).forEach(函数(k){
如果(!Array.isArray(对象)){
键[k]=(键[k]| | 0)+inc;
如果(!键[k]){
删除键[k];
}
}
if(对象[k]&&typeof对象[k]='object'){
getCount(对象[k],键,公司)
}
});
}
var object1={性别:“男人”,年龄:33岁,出生地:“美国”,工作信息:{类型:“老师”,学校:“威斯康星州”},儿童:[{姓名:“丹尼尔”,年龄:12岁,宠物:[{类型:“猫”,姓名:“威利”,年龄:2},{类型:“狗”,姓名:“吉米”,年龄:5},{姓名:“马丁”,年龄:14岁,宠物:[{类型:“鸟”,姓名:“瓦格纳”,年龄:12}],爱好:{类型:“足球”“,赞助商:{姓名:“百事”,赞助商金额:1000,联系人:{姓名:“利昂”,年龄:59岁,儿童:[{姓名:“詹姆斯”,宠物:[{类型:“狗”,年龄:4}]}}}},
对象2={性别:“男人”,工作信息:{类型:“老师”},孩子:[{姓名:“丹尼尔”,年龄:12岁,宠物:[{类型:“猫”,姓名:“威利”,年龄:2},{类型:“狗”,姓名:“吉米”,年龄:5}]},
计数={};
getCount(object1,count,1);
getCount(object2,count,-1);
控制台日志(计数)代码>
。作为控制台包装{max height:100%!important;top:0;}
您可以使用对象进行计数
函数getCount(对象,键,inc){
Object.keys(对象).forEach(函数(k){
如果(!Array.isArray(对象)){
键[k]=(键[k]| | 0)+inc;
如果(!键[k]){
删除键[k];
}
}
if(对象[k]&&typeof对象[k]==
const compareObjects = (obj1, obj2) => {
function getAllKeyNames(o, arr, str){
Object.keys(o).forEach(function(k){
if (Object.prototype.toString.call(o[k]) === "[object Object]") {
getAllKeyNames(o[k], arr, (str + '.' + k));
} else if (Array.isArray(o[k])) {
o[k].forEach(function(v){
getAllKeyNames(v, arr, (str + '.' + k));
});
}
arr.push(str + '.' + k);
});
}
function diff(arr1, arr2) {
for(let i = 0; i < arr2.length; i++) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
return arr1;
}
const o1Keys = [];
const o2Keys = [];
getAllKeyNames(obj1, o1Keys, ''); // get the keys from schema
getAllKeyNames(obj2, o2Keys, ''); // get the keys from uploaded file
const missingProps = diff(o1Keys, o2Keys); // calculate differences
for(let i = 0; i < missingProps.length; i++) {
missingProps[i] = missingProps[i].replace('.', '');
}
return missingProps;
}