3

I have an array of two JSON objects, I want to determine if they are equal. Below are example of JSON objects.

let JSON1 = {
  "products": [
    {
      "productname": "product1",
      "productversion": "1.0",
      "features": [{ "featurename": "feature1", "featureversion": "1.0" }],
    },
    {
      "productname": "product2",
      "productversion": "2.0",
      "features": [{ "featurename": "feature2", "featureversion": "2.0" }],
    },
    {
      "productname": "product3",
      "productversion": "3.0",
      "features": [{ "featurename": "feature3", "featureversion": "3.0" }],
    },
  ],
};

let JSON2 = {
  "products": [
    {
      "productname": "product2",
      "productversion": "2.0",
      "features": [{ "featurename": "feature2", "featureversion": "2.0" }],
    },
    {
      "productname": "product1",
      "productversion": "1.0",
      "features": [{ "featurename": "feature1", "featureversion": "1.0" }],
    },
    {
      "productname": "product3",
      "productversion": "3.0",
      "features": [{ "featurename": "feature3", "featureversion": "3.0" }],
    },
  ],
};

As you see I have three products and a feature associated with them, both the array are equal but just the order of array elements are different. For the above it should determine it is equal. Only If one of object missing between arrays only then it should be false.

let JSON3 = {
  "products": [
    {
      "productname": "product1",
      "productversion": "1.0",
      "features": [{ "featurename": "feature1", "featureversion": "1.0" }],
    },
    {
      "productname": "product2",
      "productversion": "2.0",
      "features": [{ "featurename": "feature2", "featureversion": "2.0" }],
    },
    {
      "productname": "product3",
      "productversion": "3.0",
      "features": [{ "featurename": "feature4", "featureversion": "4.0" }],
    },
  ],
};

let JSON4 = {
  "products": [
    {
      "productname": "product2",
      "productversion": "2.0",
      "features": [{ "featurename": "feature5", "featureversion": "5.0" }],
    },
    {
      "productname": "product1",
      "productversion": "1.0",
      "features": [{ "featurename": "feature1", "featureversion": "1.0" }],
    },
    {
      "productname": "product3",
      "productversion": "3.0",
      "features": [{ "featurename": "feature3", "featureversion": "3.0" }],
    },
  ],
};

Comparing JSON3 and JSON4 it should return false as JSON4 has different feature set which I have marked bold. How to write a script that determines if they are equal or not?

Currently, I just pull properties value to array for example I try to compare prouductName and featureName but this is not efficient way. Below is the code snippet.

I pass different objects to below script to compare the data later.

let productNames = [];
let featureNames = [];
 for(let l=0;l<products.length;l++) {
   productNames.push(products[l].productname);
    for(let k=0;k<products[l].features.length;k++) {
      featureNames.push(products[l].features[k].featurename);
    }
} 

productNames.sort();
featureNames.sort();              
karansys
  • 2,449
  • 7
  • 40
  • 78
  • can you edit your question and provide valid `JSON` data? – gorak Jun 07 '20 at 12:35
  • Hi, there are lots of questions on stackoverflow about comparing json objects. https://stackoverflow.com/questions/26049303/how-to-compare-two-json-have-the-same-properties-without-order here is one i found maybe just check those first? – halilcakar Jun 07 '20 at 12:35
  • So you only need boolean value either `true` or `false` depending on the comparison right? – gorak Jun 07 '20 at 12:39
  • Exactly, If any feature is missing or is different, I want it to be flagged as false. Basically I want to do deeper comparision – karansys Jun 07 '20 at 12:41

1 Answers1

4

With help of every() and some(), you can achieve your task.

let JSON3 = { products:[{"productname":"product1","productversion":"1.0","features":[{"featurename":"feature1","featureversion":"1.0"}] }, {"productname":"product2","productversion":"2.0","features":[{"featurename":"feature2","featureversion":"2.0"}] },{"productname":"product3","productversion":"3.0","features":[{"featurename":"feature4","featureversion":"4.0"}] }] };

let JSON4 = { products: [{"productname":"product2","productversion":"2.0","features":[{"featurename":"feature5","featureversion":"5.0"}] },{"productname":"product1","productversion":"1.0","features":[{"featurename":"feature1","featureversion":"1.0"}] },{"productname":"product3","productversion":"3.0","features":[{"featurename":"feature3","featureversion":"3.0"}] }] };

var result = JSON3.products.every(k=>JSON4.products.some(d=>d.productname ==k.productname && d.features.every(s=>k.features.some(l=>l.featurename==s.featurename))));

console.log(result);
gorak
  • 5,233
  • 1
  • 7
  • 19