我有一个JSON数组和JSON对象(其中是字符串),我想比较两者并找到不同的字段。
我已经尝试了以下可能性,并得到o / p为:
JSON响应如下:
-------rule_before
RowDataPacket {
id: 75,
input: 'day_of_week',
input_data: '0,1',
condition: 'in',
status: 'active',
extra_fields: '' }
-------filter_before
RowDataPacket {
id: 74,
when: 'match',
output: 'assign_department',
output_data: '3,1',
output_data_json: '',
status: 'active' }
-------rule_after
{ input: 'page_title',
input_data: 'Home',
extra_fields: '',
condition: 'match',
status: 'active' }
-------filter_after
{ when: 'match',
output: 'assign_agent',
output_data: '1',
output_data_json: '',
status: 'active' }
let rule_before = rule_data;
let filter_before = filter_data;
let rule_after = JSON.parse(request_data.rules);
let filter_after = JSON.parse(request_data.filters);
rule_before = rule_before[0];
filter_before = filter_before[0];
rule_after = rule_after[0];
filter_after = filter_after[0];
console.log("\n-------rule_before\n", rule_before);
console.log("\n-------filter_before\n", filter_before);
console.log("\n-------rule_after\n", rule_after);
console.log("\n-------filter_after\n", filter_after);
我想要像这样的区别
对于规则:输入:“ day_of_week” => input_data:“ Home” 对于过滤器:when:'match'=> when:'match'
答案 0 :(得分:0)
我不确定您要做什么,但是如果要获得两个对象的所有匹配项,请尝试下面的简单函数。
function diff(obj1, obj2) {
// Check if the Parameters are actually Objects
if (!(typeof(obj1) === "object" || typeof(obj2) === "object"))
return false
// Create arrays out of the Objects
const arr1 = Object.entries(obj1)
const arr2 = Object.entries(obj2)
// Instantiate an array with the matches
let result = []
arr1.forEach(el1 => {
arr2.forEach(el2 => {
// Check if it matches
if (el1[0] === el2[0] && el1[1] === el2[1]) {
// Add the match to the result
result.push(el1[1])
}
})
})
// Return the matches
return result
}
如果您不需要的话,请在评论中告诉我。
PS:如果您嵌套了对象,则此功能无效。