[英]Comparing two complex JSON arrays
I have an array of two JSON objects, I want to determine if they are equal.我有一个包含两个 JSON 对象的数组,我想确定它们是否相等。 Below are example of JSON objects.
下面是 JSON 对象的示例。
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.
仅当 arrays 之间缺少 object 之一时,才应为假。
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.比较 JSON3 和 JSON4 它应该返回 false,因为 JSON4 具有不同的功能集,我已将其标记为粗体。 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();
With help of every()
and some()
, you can achieve your task.在
every()
和some()
的帮助下,您可以完成任务。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.