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();              
4
  • can you edit your question and provide valid JSON data? Commented Jun 7, 2020 at 12:35
  • Hi, there are lots of questions on stackoverflow about comparing json objects. stackoverflow.com/questions/26049303/… here is one i found maybe just check those first? Commented Jun 7, 2020 at 12:35
  • So you only need boolean value either true or false depending on the comparison right? Commented Jun 7, 2020 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 Commented Jun 7, 2020 at 12:41

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi gorak ,that was brilliant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.