简体   繁体   English

无法验证所有嵌套的子元素长度

[英]Not able to validate all nested child elements length

I have 1 object which contains nested child like below: 我有一个包含嵌套子的对象,如下所示:

$scope.artists.materials.items[] 

Now i would have several artist which will contains list of items but in this i want to check total length of each item of artists and if mismatch found then i want to return true or false. 现在我将有几个艺术家将包含项目列表,但在此我想检查每个艺术家项目的总长度,如果发现不匹配,那么我想返回真或假。

Problem is when i dont have items for any of the artist then still i am getting false 问题是当我没有任何艺术家的物品时,我仍然会变得虚假

Idea here is to store the length of the items from the first artist and make sure all of them have that same items length. 这里的想法是存储第一位艺术家的项目长度,并确保所有项目都具有相同的项目长度。

Code: 码:

function checkItemsValidity() {
      for (var i = 1; i < $scope.artists.length; index++) {
            if ($scope.artists[i].materials.items != undefined && $scope.artists[0].materials.items) {
                if($scope.artists[i].materials.items.length != $scope.artists[0].materials.items[0].length) {
                             return false;
                }
            }        
                             return false;
        }
            return true;
    }

Case 1 :In case of only 1 artist then return true becuase no other artist to compare 案例1 :如果只有1位艺术家,则返回true,因为没有其他艺术家可以比较

Case 2 : In case of 2 artist with 2 items for both artist return true else false; 案例2 :如果艺术家有2件艺术家,两位艺术家将返回true,否则为false;

Case 3 : In case of 3 artist with 2 items for artist1 and artist 2 and 5 items for artist3 then return false; 案例3 :如果艺术家1和艺术家2有3个艺术家,艺术家2有5个项目,则返回false;

Can anybody please hlep me with this?? 有人可以请你这个吗?

As I understand you just want to check if every artist has the same number of items. 据我了解,您只想检查每位艺术家是否拥有相同数量的商品。 This code: 这段代码:

var result, materialsNumber;
for (var artist of $scope.artists) {
   var artistMaterialsNumber = artist.materials.items.length;
   if (!materialsNumber) {
       materialsNumber = artistMaterialsNumber;
   }
   result = (materialsNumber === artistMaterialsNumber);
   if (!result) {
      break;
   }
}

return result;

should be useful for that. 应该对此有用。 It remembers number of items of first artist and checks if every other artist has same number of items. 它记住第一位艺术家的项目数量,并检查每个其他艺术家是否有相同数量的项目。 In case there is artist with different item number code breaks and returns false . 如果艺术家有不同的项目编号代码中断并返回false

Hi you can try this also... 嗨,你也可以尝试这个...

var vFirstItemLength = artists[0].materials.items.length;
result = (artists.filter(function(item){return item.materials.items.length===vFirstItemLength;}).length === (artists.length));

 var artists = [{ materials: { items: [1, 2, 3] } }, { materials: { items: [1, 3] } }, { materials: { items: [1, 2, 3] } }, { materials: {} }]; artists.some(function(artist, i) { if (i === 0) return false; if (artists.length === 1) { console.log("Index " + i); console.log(true); return true; // length is one } if (artists[0].materials.items) { if (!artist.materials.items) { console.log("Index " + i); console.log(false); return false; // items doesn't exist. Return true/false, whatever works for you } else if (artist.materials.items && artist.materials.items.length === artists[0].materials.items.length) { console.log("Index " + i); console.log(true); return true; // length is equal } else { console.log("Index " + i); console.log(false); return false; // length is unequal } } else { if (artist.materials.items) { console.log("Index " + i); console.log(false); return false; // one has items, other doesn't } else { console.log("Index " + i); console.log(true); return true; // both have no items } } }); 

Why don't you try 你为什么不试试

artists.some(function(artist, i) {
    if (i === 0) return false;
    if (artists.length === 1) {
        console.log("Index " + i);
        console.log(true);
        return true; // length is one
    }
    if (artists[0].materials.items) {
        if (!artist.materials.items) {
            console.log("Index " + i);
            console.log(false);
            return false; // items doesn't exist. Return true/false, whatever works for you
        } else if (artist.materials.items &&
            artist.materials.items.length === artists[0].materials.items.length) {
            console.log("Index " + i);
            console.log(true);
            return true; // length is equal
        } else {
            console.log("Index " + i);
            console.log(false);
            return false; // length is unequal
        }
    } else {
        if (artist.materials.items) {
            console.log("Index " + i);
            console.log(false);
            return false; // one has  items, other doesn't
        } else {
            console.log("Index " + i);
            console.log(true);
            return true; // both have no items
        }

    }
});

Since all artists need to have the same number of materials... 由于所有艺术家都需要拥有相同数量的材料......

function checkMaterials (arists)
{
  if (!artists || !artists.length) { return false; }
  if (artists.length < 2)          { return true; }

  var valid = true;
  var materialCount

  try
  {
    //All artists must have the same number of materials, so we
    //can test against the number of materials that the first
    //artist has and reduce the number times we access the object
    materialCount = (artists[0].materials.items || []).length;
  }
  catch (exception)
  {
    //Object is malformed
    return false;
  }

  //Loop through the remaining artists and check how
  //many materials they have against the first artist
  for (var i = 1; i < artists.length; i++)
  {
    if (!artists[i].materials || ((artists[i].materials.items || []).length !== materialCount)
    {
      //Once one failed case is found, we can stop checking
      valid = false;
      break;
    }
  }

  return valid;
}

//Test data
var validArtists = [{
  materials: {
    items: [1, 2, 3]
  }
}, {
  materials: {
    items: [1, 3, 4]
  }
}];

var invalidArtists = [{
  materials: {
    items: [1, 2]
  }
}, {
  materials: {
    items: [3]
  }
}];

//Tests
console.log (checkMaterials (validArsists)); //Prints true
console.log (checkMaterials (invalidArtists)); //Prints false

Should solve the issue: 应该解决问题:

function checkValidity() {
    var itemsCounts = $scope.artists.map(function(artist) { return artist.materials.items.length; });
    return itemsCounts.length > 1
        ? itemsCounts.every(function(count) { return count === itemsCounts[0]; })
        : true;
}

May be you can do as follows; 可能你可以这样做;

 var artists = [{ materials: { items: [1, 2, 3] } }, { materials: { items: [1, 2] } }, { materials: { items: [] } }, { materials: { items: [1] } }, { materials: { items: [1, 2, 3] } } ]; result = artists.map(artist => artist.materials.items.length) .every(length => length === artists[0].materials.items.length); console.log(result); 

 var artists = [{ materials: { items: [1, 2, 3] } } ]; result = artists.map(artist => artist.materials.items.length) .every(length => length === artists[0].materials.items.length); console.log(result); 

Solution: 解:

function checkItemsValidity() {
  if ($scope.artists.length === 1) {
      return true;
  }

  for (var i = 0; i < $scope.artists.length; i++) {
    //this condition might be unnecessary, I assumed items can be undefined from your code.
    if (typeof $scope.artists[i].materials.items === 'undefined') {
        $scope.artists[i].materials.items = [];
    }
    if (i === 0) {
        continue;
    }
    if ($scope.artists[i].materials.items.length !== $scope.artists[0].materials.items.length) {
        return false;
    }
  }

  return true;
}

And a fiddle with some tests: https://jsfiddle.net/6x7zpkxe/1/ 还有一些测试小提琴: https//jsfiddle.net/6x7zpkxe/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM