dream123
dream123

Reputation: 43

Compare Two Json object's value, Find the Difference of objects's value and ouptut to new Json

We have two JSON data which we would like to compare and get the difference between the values of two Keys , Count in First JSON data compared with AVG_X in JSON data .

First JSON-

var myObject=[
  {
    "Customer": "A",
    "Count": 47
  },
  {
    "Customer": "B",
    "Count": 5
  },
  {
    "Customer": "C",
    "Count": 1
  }
]

Second JSON-

var myobject1=[
  {
    "Customer": "A",
    "AVG_X": 20
  },
  {
    "Customer": "B",
    "AVG_X": 4
  },
  {
    "Customer": "C",
    "AVG_X": 0
  },
  {
    "Customer": "D",
    "AVG_X": 3
  }
]

Now to compare the values the of Count vs AVG_X and get the difference between these two for each Customer and save it in new JSON .

Expected Output Should be something like below -

{
"Customer": "A", 
"Diff": 27
},
{
"Customer": "B", 
"Diff": 1 
},
"Customer": "C", 
"Diff": 1 
}

Upvotes: 0

Views: 1214

Answers (2)

pullidea-dev
pullidea-dev

Reputation: 1803

var myObject=[
  {
    "Customer": "A",
    "Count": 47
  },
  {
    "Customer": "B",
    "Count": 5
  },
  {
    "Customer": "C",
    "Count": 1
  }
];

var myobject1=[
  {
    "Customer": "A",
    "AVG_X": 20
  },
  {
    "Customer": "B",
    "AVG_X": 4
  },
  {
    "Customer": "C",
    "AVG_X": 0
  },
  {
    "Customer": "D",
    "AVG_X": 3
  }
];

let myobject1_customer_array = myobject1.map(a => a.Customer);

let result = [];
let myobject1_id;

for(var i = 0; i < myObject.length; i++) {

  myobject1_id = myobject1_customer_array.indexOf(myObject[i].Customer);

  if (myobject1_id != -1) {
    
    result.push({
      "Customer": myObject[i].Customer,
      "Diff": Math.abs(myObject[i].Count - myobject1[myobject1_id].AVG_X)
    })
  }
}

console.log(result);

Upvotes: 1

DecPK
DecPK

Reputation: 25401

Just map over the myObject and get the difference and store the result in a new object with properties Customer and Diff.

If the object with same Customer in both array

var myObject = [
  { Customer: "A", Count: 47 },
  { Customer: "B", Count: 5 },
  { Customer: "C", Count: 1 },
];

var myobject1 = [
  { Customer: "A", AVG_X: 20 },
  { Customer: "B", AVG_X: 4 },
  { Customer: "C", AVG_X: 0 },
  { Customer: "D", AVG_X: 3 },
];

const result = myObject.map((o, i) => ({
  Customer: o.Customer,
  Diff: o.Count - myobject1[i].AVG_X,
}));

console.log(result);

If the object can be present in any position in myobject1

var myObject = [
  { Customer: "A", Count: 47 },
  { Customer: "B", Count: 5 },
  { Customer: "C", Count: 1 },
];

var myobject1 = [
  { Customer: "A", AVG_X: 20 },
  { Customer: "B", AVG_X: 4 },
  { Customer: "C", AVG_X: 0 },
  { Customer: "D", AVG_X: 3 },
];

const result = myObject.map((obj) => {
  const { Customer, Count } = obj;
  const isExist = myobject1.find((o) => o.Customer === Customer);
  if (isExist) return { Customer, Diff: Count - isExist.AVG_X };
  else return obj;
});

console.log(result);

Upvotes: 1

Related Questions