locked
check array for data consistency RRS feed

  • Question

  • User2108892867 posted

    Hello everyone, I have this array that I would need to check for data consistency. So here how it looks like:

    [
    {
    rowIndex:0,
    data: "Test"
    },
    {
    rowIndex:0,
    data: "Test 1"
    },
    {
    rowIndex:0,
    data: "Test 2"
    },
    {
    rowIndex:1,
    data: null
    },
    {
    rowIndex:1,
    data: null
    },
    {
    rowIndex:1,
    data: null
    },
    {
    rowIndex:2,
    data: "My test 1"
    },
    {
    rowIndex:2,
    data: "My test 2"
    },
    {
    rowIndex:2,
    data: "My test 3"
    },
    ]

    This array will go through some modification by the users and I need some ways to check that the data in all the objects either have or not have data anything in it if they have the same rowIndex number. For instance if the array came back this way: 

    [
    {
    rowIndex:0,
    data: null
    },
    {
    rowIndex:0,
    data: "Test 1"
    },
    {
    rowIndex:0,
    data: "Test 2"
    },
    {
    rowIndex:1,
    data: null
    },
    {
    rowIndex:1,
    data: null
    },
    {
    rowIndex:1,
    data: null
    },
    {
    rowIndex:2,
    data: "My test 1"
    },
    {
    rowIndex:2,
    data: "My test 2"
    },
    {
    rowIndex:2,
    data: "My test 3"
    },
    ]

    This is not valid because the first object with rowIndex = 0, the data was modified to null but there are other two objects also have rowIndex = 0, the data are not null. Pretty much if they have the same rowIndex, the data can be either something or nothing at the same time. How could I check that?

    Thanks. 

     

    Wednesday, January 16, 2019 3:34 AM

All replies

  • User1520731567 posted

    Hi asplearning,

    According to your description,the judgment condition I understand is this

    1.

    xxxx

    null

    xxxx

    ====>Inconsistent

    2.

    xxxx

    xxxx

    xxxx

    ====>Consistent

    3.

    null

    null

    null

    ====>Consistent

    If so,I think you could use groupby rowIndex in linq,

    and determine whether the number of null is 0 or equal to the total number of records under the same rowIndex.

    For example:

      public class dataVM
            {
                public int rowIndex { get; set; }
                public string data { get; set; }
    
            }
                List<dataVM> model = new List<dataVM>();
                model.Add(new dataVM { rowIndex = 0, data = null,       });
                model.Add(new dataVM { rowIndex = 0, data = "Test 1"   });
                model.Add(new dataVM { rowIndex = 0, data = "Test 2"  });  //====>Inconsistent
                model.Add(new dataVM { rowIndex = 1, data = null       });
                model.Add(new dataVM { rowIndex = 1, data = null       });
                model.Add(new dataVM { rowIndex = 1, data = null       });  //====>Consistent
                model.Add(new dataVM { rowIndex = 2, data = "My test 1" });
                model.Add(new dataVM { rowIndex = 2, data = "My test 2" });
                model.Add(new dataVM { rowIndex = 2, data = "My test 3" });  //====>Consistent
    
            var vaildResult = model.GroupBy(_ => _.rowIndex, (key, group) => new { rowIndex = key, data = group.ToList().Select(a => a.data), vaild = (group.ToList().Where(p => p.data == null).Count() == 0 || group.ToList().Where(p => p.data == null).Count() == group.ToList().Count()) ? "Consistent" : "Inconsistent" }).ToList();
                

    How it works in my project:

    Best Regards.

    Yuki Tao

    Wednesday, January 16, 2019 8:51 AM
  • User2108892867 posted

    Yes what you pointed out is correct. I am sorry. I thought it's clear that I wanted to do this in javascript or jquery cause I posted it under javascript tag.  I would like to loop through the whole array, if there is inconsistency in any object, I want to return valid as false. If everything is consistent, then return valid as true.  

    Wednesday, January 16, 2019 8:56 AM
  • User-474980206 posted

    pretty simple:

    assume the array is named data:

    // 1) create array of arrays indexed by rowIndex
    // 2) map array to valid by rowindex
    //    rowIndex is valid if all null or all have index
    var rowsValid = data
      .reduce(function(a,c){
        a[c.rowIndex] = a[c.rowIndex] ? a[c.rowIndex] : [];
        a[c.rowIndex].push(c.data);
        return a;
      },[])
      .map(function(c) {
        var valCount = c.reduce(function(a,c){
          return c ? a+1 : a;
        },0);
        return valCount == 0 || valCount == c.length;
      });
    
    // check if all rows valid
    var allValid = rowsValid.reduce(function(a,c) {return a && c; }, true);
    

    Wednesday, January 16, 2019 4:28 PM
  • User2108892867 posted

    Thanks Bruce for your solution. Now another requirement came up. The array structure changed a little bit. Here how it looks:

    [
    {
    rowIndex:0,
    data: null, 
    selectedValue: 1
    },
    {
    rowIndex:0,
    data: "Test 1", 
    selectedValue: null
    },
    {
    rowIndex:0,
    data: "Test 2", 
    selectedValue: null
    },
    {
    rowIndex:1,
    data: null, 
    selectedValue: null
    },
    {
    rowIndex:1,
    data: null, 
    selectedValue: null
    },
    {
    rowIndex:1,
    data: null, 
    selectedValue: null
    },
    {
    rowIndex:2,
    data: null, 
    selectedValue: 2
    },
    {
    rowIndex:2,
    data: "My test 2", 
    selectedValue: null
    },
    {
    rowIndex:2,
    data: "My test 3", 
    selectedValue: null
    },
    ]

    Now there is another property called selectedValue and the above array is valid cause although data is null, selectedValue is not null and that makes rowIndex =0 valid. 

    Thanks. 

     

    Thursday, January 17, 2019 12:21 AM
  • User-474980206 posted
    Simple, change push to:

    a[c.rowIndex].push(c.data || c.selectedValue);
    Thursday, January 17, 2019 1:26 AM
  • User2108892867 posted

    Thank you Bruce. I will try that. Sorry I am trying to make sense of your code. Could you explain this inline condition please?

     a[c.rowIndex] = a[c.rowIndex] ? a[c.rowIndex] : [];

    What does it mean or do?

    Thursday, January 17, 2019 1:55 AM