In fact, I could not find the code to check whether the object is an array of length (for example, an empty array) anywhere, so I wrote it very quickly if someone wants it later:
function isArrayOfLength(obj, length) { var isArrayOfSpecifiedLength = false; if(Array.isArray(obj)){ if(obj.length === length){ isArrayOfSpecifiedLength = true; } } return isArrayOfSpecifiedLength; }
And tests for him:
(function(){ var isValidArray = isArrayOfLength([], 0); console.log(isValidArray); //true })(); (function(){ var isValidArray = isArrayOfLength([1,2,3], 0); console.log(isValidArray); //false })(); (function(){ var isValidArray = isArrayOfLength([1,2,3], 3); console.log(isValidArray); //true })(); (function(){ var isValidArray = isArrayOfLength({notAnArray:true}, 0); console.log(isValidArray); //false })();
https://plnkr.co/edit/sjgnVoNAI0KUNCwhbdpx?p=preview
VSO
source share