2016-09-30 56 views
4

我有1个对象包含了像下面嵌套子:无法验证所有嵌套子元素长度

$scope.artists.materials.items[] 

现在我有几个艺术家这将包含的项目列表,但在这我要检查总每个艺术家项目的长度,如果发现不匹配,那么我想返回true或false。

Problem is when i dont have items for any of the artist then still i am getting false

这里的想法是存储来自第一位艺术家的物品的长度,并确保他们都具有相同的物品长度。

代码:

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; 
    } 

案例1:在只有1艺人的情况,则返回true监守没有其他艺术家比较

案例2:在2艺术家的情况下,与两个艺术家的物品返回true否则为false;

案例3:如果是3位艺术家,其中艺术家2和艺术家2的2个项目和艺术家3的5个项目,则返回false;

任何人都可以请这个我?

+0

为了澄清,你想看看每个艺术家只是有相同数量的项目在他们的材料对象? –

+0

@dominic aquilina是的,这是正确的。每个艺术家应该有相同数量的项目。如果没有艺术家有项目,那么函数应该返回true,或者如果所有艺术家都有相同数量的项目,那么函数应该返回true否则为false –

回答

1

由于所有的艺术家需要有相同数量的材料......

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 
+0

为什么我们要在try块外进行循环,除此之外,为什么您要检查艺术家的长度而不是项目? ? –

+1

try块用于防御第一个元素上的格式错误的数据。之后,它与自己的卫兵进行比较,但在第一个元素上添加相同的警卫将效率较低。而艺术家的长度只是对输入的额外保障。如果没有什么可以测试的话,测试就会失败。如果只有一位艺术家,那么测试就会立即成功,因为它很简单。 –

+0

您的解决方案能够发挥作用。非常感谢您为您节省宝贵的时间,并帮助我,请继续帮助我:) –

1

var artists = [{ 
 
\t materials: { 
 
\t \t items: [1, 2, 3] 
 
\t } 
 
}, { 
 
\t materials: { 
 
\t \t items: [1, 3] 
 
\t } 
 
}, { 
 
\t materials: { 
 
\t \t items: [1, 2, 3] 
 
\t } 
 
}, { 
 
\t materials: {} 
 
}]; 
 

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

 
\t } 
 
});

你为什么不尝试

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 
     } 

    } 
}); 
+0

这根本不起作用,因为当我没有项目任何材料在这种情况下,我越来越错误:$ scope.artists [i] .materials.items是undefined –

+0

更新我的回答 – nikjohn

+0

我只想验证artist.If两侧的项目,如果任何艺术家项目是不匹配,那么只我想返回false.With你的函数,如果第一艺术家和第二艺术家的项目匹配,但第三艺术家项目不匹配,那么我会变得真实。我想在最后返回true,同时在所有艺术家之间进行各种比较项目 –

4

据我了解你只是想检查每个艺术家是否有相同数量的项目。此代码:

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; 

应该对此有用。它记得第一个艺术家的项目数量,并检查每个其他艺术家是否具有相同数量的项目。如果艺术家使用不同的商品编号代码中断并返回false

+0

非常感谢您为您的宝贵时间节省时间和帮助我请保持这样的帮助:) –

1

要解决这个问题:

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; 
} 
+0

非常感谢你从繁忙的日程安排中节省了宝贵的时间,并帮助我,请继续帮助我:) –

1

可能是你可以做如下:

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);

+0

非常感谢您为了节省宝贵的时间,请继续这样的帮助:) –

2

嗨,你也可以试试这个...

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

非常感谢您在百忙之中节省您宝贵的时间,并帮助我,请继续帮助像这样:) –

1

解决方案:

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; 
} 

并与一些测试小提琴:https://jsfiddle.net/6x7zpkxe/1/

+0

非常感谢您在百忙之中节省宝贵的时间,并帮助我,请保持帮助像这样:) –