Home > Blockchain >  How to validate a list of objects value
How to validate a list of objects value

Time:02-21

My array look like this:

var theset=[
    {
        "set": "1",
        "data": [
            { "field": "A", "value": "111", "check": true },
            { "field": "B", "value": "111", "check": false },
            { "field": "C", "value": "111", "check": true }     
        ]
    },
    {
        "set": "2",
        "data": [
            { "field": "A", "value": "111", "check": true },
            { "field": "B", "value": "222", "check": false },
            { "field": "C", "value": "222", "check": true }     
        ]
    },
    {
        "set": "3",
        "data": [
            { "field": "A", "value": "333", "check": true },
            { "field": "B", "value": "333", "check": false },
            { "field": "C", "value": "222", "check": true }     
        ]
    },
    {
        "set": "4",
        "data": [
            { "field": "A", "value": "444", "check": true },
            { "field": "B", "value": "333", "check": false },
            { "field": "C", "value": "444", "check": true }     
        ]
    }
];

What I want to do is validate the "value" with other set on the same field only when the "check" is true.

The result is to return a true if there is a duplication of "value" in the set. The example will return a true because

  • set 1: having duplicate value for field A with set 2
  • set 2: having duplicate value for field A with set 1, duplicate value for field C with set 3
  • set 3: having duplicate value for field C with set 2
  • set 4: not consider as duplicate, even though the value of field B match with set 3 because of the check is false

so far I tried to do for loop on the list but this will have a lot of nested loop which is not efficient.

for(var i=0; i<theset.length; i  ){
    var checking = theset[i].data;
    for(var j=0; j<checking.length; j  ){
        if(checking[j].check){
            for(var k=0; k<theset.length; k  ){
                if(k!=i){
                    var checking2 = theset[k].data;
                    for(var l=0; l<checking2.length; l  ){
                     ...
                    }
                }
            }         
        }
    }
}

Can anybody help me?

CodePudding user response:

one way can be for each set to filter the duplicate data with array.filter

then if there is duplicate do the wanted treatment

var theset = [
    {
        "set": "1",
        "data": [
            { "field": "A", "value": "111", "check": true },
            { "field": "B", "value": "111", "check": false },
            { "field": "C", "value": "111", "check": true }     
        ]
    },
    {
        "set": "2",
        "data": [
            { "field": "A", "value": "111", "check": true },
            { "field": "B", "value": "222", "check": false },
            { "field": "C", "value": "222", "check": true }     
        ]
    },
    {
        "set": "3",
        "data": [
            { "field": "A", "value": "333", "check": true },
            { "field": "B", "value": "333", "check": false },
            { "field": "C", "value": "222", "check": true }     
        ]
    },
    {
        "set": "4",
        "data": [
            { "field": "A", "value": "444", "check": true },
            { "field": "B", "value": "333", "check": false },
            { "field": "C", "value": "444", "check": true }     
        ]
    }
];

theset.forEach(set => {
  var duplicate = set.data.filter(data => {
    return theset.some(oneSet => oneSet.data.some(oneData => oneData.value === data.value));
  });
  
  if (duplicate.length) {
    console.log(`the following set have duplicate ${set.set}`);
    console.log(duplicate);
    //treat as you want the set and the duplicate
  }
});

CodePudding user response:

i would do it with a hash map

const theset = [
    {
        "set": "1",
        "data": [
            { "field": "A", "value": "111", "check": true },
            { "field": "B", "value": "111", "check": false },
            { "field": "C", "value": "111", "check": true }     
        ]
    },
    {
        "set": "2",
        "data": [
            { "field": "A", "value": "111", "check": true },
            { "field": "B", "value": "222", "check": false },
            { "field": "C", "value": "222", "check": true }     
        ]
    },
    {
        "set": "3",
        "data": [
            { "field": "A", "value": "333", "check": true },
            { "field": "B", "value": "333", "check": false },
            { "field": "C", "value": "222", "check": true }     
        ]
    },
    {
        "set": "4",
        "data": [
            { "field": "A", "value": "444", "check": true },
            { "field": "B", "value": "333", "check": false },
            { "field": "C", "value": "444", "check": true }     
        ]
    }
];

const hashmap = {};

theset.forEach((item) => {
    item.data.forEach((obj) => {
        if (!obj.check) {
            return;
        }
        const key = JSON.stringify(obj);
        if (!hashmap[key]) {
            hashmap[key] = [];
        }
        
        hashmap[key].push(item.set);
    })
})

console.log('hashmap', hashmap)

as a result you would get the following output:

{
  "obj1": ["1","2"],
  "obj2": ["1"],
  "obj3": ["2","3"],
  "obj4": ["3"],
  "obj5": ["4"],
  "obj6": ["4"]
}
  • obj1 is common for set 1 and set 2
  • obj3 is common for set 2 and set 3

you have the all needed information for your conclusions

of course, you could use a custom hash function rather than JSON.stringify()

  • Related