64631

Comparing two complex JSON arrays

<h3>Question</h3>

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();
<h3>Answer1:</h3>

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

<pre class="snippet-code-js lang-js prettyprint-override">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);

来源:https://stackoverflow.com/questions/62245387/comparing-two-complex-json-arrays

Recommend