-2

I've one JSON array and JSON object (Which is string) and I wanted to compare and find different field from both.

I've tried below possibilities and get o/p as:

JSON Response is as:

-------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);

I want the difference like

for rule: input: 'day_of_week' => input_data: 'Home' for filter: when: 'match' => when: 'match'

Kunal Dholiya
  • 335
  • 2
  • 6
  • 19

1 Answers1

1

I'm not really sure what you are trying do to but if you want to get all matches of two Objects try the simple function below.

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: If you have nested Objects this function doesn't work.

chachli
  • 93
  • 9