2016-09-30 78 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

非常感謝您在百忙之中節省寶貴的時間,並幫助我,請保持幫助像這樣:) –