Trying to find if array is full or not - javascript

I'm working with JQuery to determine if an array, built earlier, with a determined number of indexes, is full or not.
When created, array looks like this :
,,,,,,
All I want to do is find if every position is filled or not.
So bascially, i'm trying to test if and only if
[x,x,x,x,x,x]
For example, if
[x,x,x,,x,x] or if [x,,,,,] //return false, wrong...
Thanks a lot.

You don't need any specific jQuery stuff to read the size of an array. Just vanilla javascript has what you need.
var arrayMaxValues = 4;
var testArray = [1,2,3,4];
if ( testArray.length == arrayMaxValues )
{
alert( 'Array is full!' );
}
else if ( testArray.length > arrayMaxValues )
{
alert( 'Array is overstuffed!' );
} else {
alert( 'There's plenty of room!' );
}
EDIT
Ah, you re-typed your question. So, you want to see if they array has zero null or undefined values?
function isArrayFull( arr )
{
for ( var i = 0, l = arr.length; i < l; i++ )
{
if ( 'undefined' == typeof arr[i] || null === arr[i] )
{
return false
}
}
return true;
}

If you fill your initial array with nulls, like:
const arr = Array(9).fill(null);
Then you can use some() or every() like this:
// check if a null doesn't exist
!arr.some(element => element === null)
// check if every element is not null
arr.every(element => element !== null)
Use whichever you like, since both of them breaks the loop when a null is found.

I know this is doing the same as Peter Bailey's function, but I only wanted to share it since it's a different approach using the built-in power of array filtering.
So, since the full array has a predetermined length you could filter it against 'undefined' and null and compare lengths, like this:
function isArrayFull(arr) {
return arr.length === arr.filter(function(o) {
return typeof o !== 'undefined' || o !== null;
}).length;
}

Related

filter to get rid of blank array entries gives no error, but does not work (javascript)

I hope I just missing something simple, but I for the life of me cant see why this doesn't work:
// GET ALL SHEET NAMES FROM THE SPREADSHEET AND PUT INTO AN ARRAY
function sheetNames() {
var out = new Array()
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i=0 ; i<sheets.length ; i++) {
out.push( [ checkSheetName(sheets[i].getName()) ] )
}
Logger.log("before : " + out);
var listOfsheets = out.filter(function(x){ return (x !== (undefined || null || '')); });
Logger.log("after : " + listOfsheets);
return listOfsheets ;
}
// SEARCH ALL SHEETS AND ONLY SELECT THEM IF THEY HAVE THE TEXT "REPORT_" IN THE NAME
function checkSheetName(sheetName) {
var checkFor = 'Report_'
if (sheetName.indexOf(checkFor) >= 0) {
return sheetName;
} else {
sheetName = '';
return sheetName;
}
My logger shows the exact same result before and after its been put through the filter. An error would be great, but runs fine... just doesn't seem to do anything.
I suppose another question would be, can I put something in the initial getSheets() so that it only gets what I need in the first place? But Even if that is possible, I'm still curious about why my filter doesn't work
x is an array rather than an array-item string, make it
out.push( checkSheetName(sheets[i].getName()) ); //observe that array wrapping is removed
You can shorten your code by doing (assuming that sheets is array-like, not a direct array)
return Array.from( sheets ).filter( x => !!checkSheetName( x.getName() ) );
If sheets is an array then make it
return sheets.filter( x => !!checkSheetName( x.getName() ) );
or even without checkSheetName
return sheets.filter( x => !!( x.getName().indexOf( "Report_" ) != -1 ) );
out.filter(function(x){ return (x !== (undefined || null || '')); });
Your filter function reduces to
out.filter(function (x) { return (x !== ''); });
because the engine looks inside the parentheses and parses from left to right:
(x !== (undefined || null || ''))
// undefined is falsy, replace with expression on the right
(x !== (null || ''))
// null is falsy, replace with expression on the right
(x !== '')
Now look at what you're filtering:
out.push( [ checkSheetName(sheets[i].getName()) ] )
Here, you push a new array with one element onto the out array. Drop the square brackets to just push the name.
Since x is always an array, it is never the empty string, and your filter filters out nothing. Even if you drop the brackets making x an array, if x is null or undefined, it will still pass your filter. Replace your filter with
out.filter(Boolean);
This will coerce your array value to a boolean. The empty string, null, and undefined are all falsy and return false. Other strings are truthy and return true, the exact filtering you need.

How do you check if a property value is an array within an object?

If we have:
var obj = {
key: [1000, 10, 50, 10]
};
-If the array at the given key is empty, it should return 0.
-If the property at the given key is not an array, it should return 0.
-If there is no property at the given key, it should return 0.
I'm trying to get the average of the elements at the property (key) with a function, getAverageOfElementsAtProperty(obj, 'key').. I managed that part with exception of the 3 points above.
I tried this:
if (obj[key].constructor != Array || !obj.hasOwnProperty(key) ||
obj[key] == []) {
return 0;
}
But I'm unsure if using three or operational is the correct move...
You can check multiple conditions like this
if (
typeof obj === 'object' && // you have an object
'key' in object && // it contains a "key"
Array.isArray( obj['key'] ) // it is an array
)
So for each condition you mentioned it can be done as follows
If the array at the given key is empty, it should return 0.
obj.key.length === 0
If the property at the given key is not an array, it should return 0.
!Array.isArray(obj[key])
If there is no property at the given key, it should return 0.
!obj.hasOwnProperty("key").
You can directly check for a falsy value to check the existence. Also, check if the the value is an array by using the Array.isArray function and then for checking the length of array, use the length property.
if(!obj.hasOwnProperty("key") || !Array.isArray(obj[key]) || obj.key.length === 0)){
return 0;
}
I like to break up my if into the business logic that it needs to fulfill. I find it easier to understand when I get back to it later on. So I would do it this way
if (!obj.hasOwnProperty(key)) return 0; // If there is no property at the given key
if (!Array.isArray(obj[key])) return 0; // If the property at the given key is not an array
if (obj[key].length === 0) return 0; // If the array at the given key is empty
// add this point we know there is a valid array
You can check, first, [1] if the key exists, then [2] if it's array, and, then, [3] if it's not empty.
Order matters: due to Short-circuit evaluation, if it evaluates true in the first condition, it skips nexts checks (then, you can safely assume that the key exists, that it's an array and so on...)
var obj = {
key: [1000, 10, 50, 10],
key2: [],
key3: "abc"
};
function isValid(obj, key) {
if (
(typeof obj[key] === 'undefined') || //If there is no property at the given key (first)
(!Array.isArray(obj[key])) || //If the property at the given key is not an array (IE9>=)
(obj[key].length == 0) //If the array at the given key is empty,
) {
return 0;
} else {
return 1; /* or whathever you want */
}
}
console.log(isValid(obj, "key")); //1
console.log(isValid(obj, "key2")); //0
console.log(isValid(obj, "key3")); //0
console.log(isValid(obj, "key4")); //0
Note that (typeof obj[key] === 'undefined') is not necessary here, because Array.isArray(undefined) === false (check docs), so, you can skip this check.
if (obj[key].constructor != Array || ... will throw an error if obj[key] is undefined because, then, you'll be accessing constructor of undefined which will throw an error. What you need to do is check if a the value obj[key] exist then if it is an array like this:
if(obj[key] !== undefined || obj[key].constructor != Array)
return 0;
In general: The or (||) operator will stop checking (evaluating) after the first valid check. Consider check0 || check1 || ... || checkN, if checki is true then checki+1 ... checkN won't be evaluated at all. Because only one valid check is sufficient. Here is an example:
var n = 5;
if(n == 5 || console.log("Here 1"))
;
n = 4;
if(n == 5 || console.log("Here 2"))
;
As you can see, Here 1 is never loged because the code console.log("Here 1") is never reached.

How to check - $scope.students = []; - in JavaScript - Angular JS [duplicate]

When the page is loading for the first time, I need to check if there is an image in image_array and load the last image.
Otherwise, I disable the preview buttons, alert the user to push new image button and create an empty array to put the images;
The problem is that image_array in the else fires all time. If an array exists - it just overrides it, but alert doesn't work.
if(image_array.length > 0)
$('#images').append('<img src="'+image_array[image_array.length-1]+'" class="images" id="1" />');
else{
$('#prev_image').attr('disabled', 'true');
$('#next_image').attr('disabled', 'true');
alert('Please get new image');
var image_array = [];
}
UPDATE
Before loading html, I have something like this:
<?php if(count($images) != 0): ?>
<script type="text/javascript">
<?php echo "image_array = ".json_encode($images);?>
</script>
<?php endif; ?>
if (typeof image_array !== 'undefined' && image_array.length > 0) {
// the array is defined and has at least one element
}
Your problem may be happening due to a mix of implicit global variables and variable hoisting. Make sure you use var whenever declaring a variable:
<?php echo "var image_array = ".json_encode($images);?>
// add var ^^^ here
And then make sure you never accidently redeclare that variable later:
else {
...
image_array = []; // no var here
}
To check if an array is either empty or not
A modern way, ES5+:
if (Array.isArray(array) && array.length) {
// array exists and is not empty
}
An old-school way:
typeof array != "undefined"
&& array != null
&& array.length != null
&& array.length > 0
A compact way:
if (typeof array != "undefined" && array != null && array.length != null && array.length > 0) {
// array exists and is not empty
}
A CoffeeScript way:
if array?.length > 0
Why?
Case Undefined
Undefined variable is a variable that you haven't assigned anything to it yet.
let array = new Array(); // "array" !== "array"
typeof array == "undefined"; // => true
Case Null
Generally speaking, null is state of lacking a value. For example a variable is null when you missed or failed to retrieve some data.
array = searchData(); // can't find anything
array == null; // => true
Case Not an Array
Javascript has a dynamic type system. This means we can't guarantee what type of object a variable holds. There is a chance that we're not talking to an instance of Array.
supposedToBeArray = new SomeObject();
typeof supposedToBeArray.length; // => "undefined"
array = new Array();
typeof array.length; // => "number"
Case Empty Array
Now since we tested all other possibilities, we're talking to an instance of Array. In order to make sure it's not empty, we ask about number of elements it's holding, and making sure it has more than zero elements.
firstArray = [];
firstArray.length > 0; // => false
secondArray = [1,2,3];
secondArray.length > 0; // => true
How about (ECMA 5.1):
if(Array.isArray(image_array) && image_array.length){
// array exists and is not empty
}
This is what I use. The first condition covers truthy, which has both null and undefined. Second condition checks for an empty array.
if(arrayName && arrayName.length > 0){
//do something.
}
or thanks to tsemer's comment I added a second version
if(arrayName && arrayName.length)
Then I made a test for the second condition, using Scratchpad in Firefox:
var array1;
var array2 = [];
var array3 = ["one", "two", "three"];
var array4 = null;
console.log(array1);
console.log(array2);
console.log(array3);
console.log(array4);
if (array1 && array1.length) {
console.log("array1! has a value!");
}
if (array2 && array2.length) {
console.log("array2! has a value!");
}
if (array3 && array3.length) {
console.log("array3! has a value!");
}
if (array4 && array4.length) {
console.log("array4! has a value!");
}
which also proves that if(array2 && array2.length) and if(array2 && array2.length > 0) are exactly doing the same
optional chaining
As optional chaining proposal reached stage 4 and is getting wider support, there is a very elegant way to do this
if(image_array?.length){
// image_array is defined and has at least one element
}
You should use:
if (image_array !== undefined && image_array.length > 0)
If you want to test whether the image array variable had been defined you can do it like this
if(typeof image_array === 'undefined') {
// it is not defined yet
} else if (image_array.length > 0) {
// you have a greater than zero length array
}
JavaScript
( typeof(myArray) !== 'undefined' && Array.isArray(myArray) && myArray.length > 0 )
Lodash & Underscore
( _.isArray(myArray) && myArray.length > 0 )
You can use jQuery's isEmptyObject() to check whether the array contains elements or not.
var testArray=[1,2,3,4,5];
var testArray1=[];
console.log(jQuery.isEmptyObject(testArray)); //false
console.log(jQuery.isEmptyObject(testArray1)); //true
Source: https://api.jquery.com/jQuery.isEmptyObject/
Using undescore or lodash:
_.isArray(image_array) && !_.isEmpty(image_array)
A simple way that doesn't result in exceptions if not exist and convert to boolean:
!!array
Example:
if (!!arr) {
// array exists
}
How about this ? checking for length of undefined array may throw exception.
if(image_array){
//array exists
if(image_array.length){
//array has length greater than zero
}
}
The best is to check like:
let someArray: string[] = [];
let hasAny1: boolean = !!someArray && !!someArray.length;
let hasAny2: boolean = !!someArray && someArray.length > 0; //or like this
console.log("And now on empty......", hasAny1, hasAny2);
See full samples list:
I come across this issue quite a lot in Javascript. For me the best way to do it is to put a very broad check before checking for length. I saw some other solutions in this Q&A, but I wanted to be able to check for either null or undefined or any other false value.
if(!array || array.length == 0){
console.log("Array is either empty or does not exist")
}
This will first check for undefined, null, or other false values. If any of those are true, it will complete the boolean as this is an OR. Then the more risky check of array.length, which could error us if array is undefined, can be checked. This will never be reached if array is undefined or null, so the ordering of conditions is very important.
If you do not have a variable declared as array you can create a check:
if(x && x.constructor==Array && x.length){
console.log("is array and filed");
}else{
var x= [];
console.log('x = empty array');
}
This checks if variable x exists and if it is, checks if it is a filled array. else it creates an empty array (or you can do other stuff);
If you are certain there is an array variable created there is a simple check:
var x = [];
if(!x.length){
console.log('empty');
} else {
console.log('full');
}
You can check my fiddle here with shows most possible ways to check array.
The following is my solution wrapped in a function that also throws
errors to manage a couple of problems with object scope and all types
of possible data types passed to the function.
Here's my fiddle used to examine this problem (source)
var jill = [0];
var jack;
//"Uncaught ReferenceError: jack is not defined"
//if (typeof jack === 'undefined' || jack === null) {
//if (jack) {
//if (jack in window) {
//if (window.hasOwnP=roperty('jack')){
//if (jack in window){
function isemptyArray (arraynamed){
//cam also check argument length
if (arguments.length === 0) {
throw "No argument supplied";
}
//console.log(arguments.length, "number of arguments found");
if (typeof arraynamed !== "undefined" && arraynamed !== null) {
//console.log("found arraynamed has a value");
if ((arraynamed instanceof Array) === true){
//console.log("I'm an array");
if (arraynamed.length === 0) {
//console.log ("I'm empty");
return true;
} else {
return false;
}//end length check
} else {
//bad type
throw "Argument is not an array";
} //end type check
} else {
//bad argument
throw "Argument is invalid, check initialization";;
}//end argument check
}
try {
console.log(isemptyArray(jill));
} catch (e) {
console.log ("error caught:",e);
}
the way I found to work (comming from another language) is to make a simple function to test.
create a function that check the size of the array and pass the lenght by parameter.
isEmpty(size){
if(size==0) {
return true;
} else {
return false;
}
}
//then check
if(isEmpty(yourArray.length)==true){
//its empty
} else {
//not empty
}
You should do this
if (!image_array) {
// image_array defined but not assigned automatically coerces to false
} else if (!(0 in image_array)) {
// empty array
// doSomething
}
For me sure some of the high rated answers "work" when I put them into jsfiddle, but when I have a dynamically generated amount of array list a lot of this code in the answers just doesn't work for ME.
This is what IS working for me.
var from = [];
if(typeof from[0] !== undefined) {
//...
}
Notice, NO quotes around undefined and I'm not bothering with the length.
Probably your image_array is not array but some OBJECT with length property (like string) - try
if(image_array instanceof Array && image_array.length)
function test(image_array) {
if(image_array instanceof Array && image_array.length) {
console.log(image_array,'- it is not empty array!')
} else {
console.log(image_array,'- it is empty array or not array at all!')
}
}
test({length:5});
test('undefined');
test([]);
test(["abc"]);
In my case, array_.length always returned 0, even if it had values inside. Probably, because of non-default indexes.
So to check if array is defined we use typeof _array !== 'undefined'
And then to check if it contains any date i just simply compare it to an empty array _array !== []
in ts
isArray(obj: any)
{
return Array.isArray(obj)
}
in html
(photos == undefined || !(isArray(photos) && photos.length > 0) )
When you create your image_array, it's empty, therefore your image_array.length is 0
As stated in the comment below, i edit my answer based on this question's answer) :
var image_array = []
inside the else brackets doesn't change anything to the image_array defined before in the code

IndexOf in arrays of objects in Node

I am not sure on the use of indexOf in arrays of objects
The code which is not working is:
if (res.locals.company.companies.indexOf(req.query.companyId) >= 0) return next()
The if condition will always return false.
I also tested in console and it is actually wrong:
>> var zio = { __v: 1,
_id: '50bc0238049a1ff10b000001',
companies:
[ { _id: '50bc01938f164ee80b000001', name: 'Test' },
{ _id: '50bc01ac4e860ee90b000001', name: 'zio' } ],
}
>> zio.companies.indexOf("50bc01938f164ee80b000001")
-1
whereas it should be true.
Should I use any mysterious underscore utility ?
UPDATE/Clarification: my aim is just to check if 50bc01938f164ee80b000001 exists in one of the ids, I don't need to know where it actually is. This is very performance critical!
Nodejs solutions or tips would be amazing!
It's not wrong. That Array does not contain a String like that, but only two Object references. Hence, the result is correctly -1.
To get the index from the Object reference containing the searched string value, we could go like
var index;
zio.companies.some(function( obj, idx ) {
if( obj._id === '50bc01938f164ee80b000001' ) {
index = idx;
return true;
}
});
console.log('index is: ', index);
Based on your ninja edit, if you just want to know whether or not an object ref holding a specific id is contained by that array, use it like
var check = zio.companies.filter(function( obj ) {
return obj._id === '50bc01938f164ee80b000001';
});
if( check.length ) {
console.log('yep');
} else {
console.log('nope');
}
Second edit: If you are really and only after performance, you probably don't want to have any function call overhead in any search. I'd use something like
function inObject(arr, search) {
var len = arr.length;
while( len-- ) {
if(arr[len]._id === search)
return true;
}
}
if( inObject( zio.companies, 'AAA' ) ) {
}
That code outclasses any snippet provided here by a few magnitudes. See Here
You'll need to loop over the elements and check for the _id being equal.
indexOf checks for strict equality, and those objects are of course not equal to that string. (It's the same logic as "hello" === {foo: "hello"}. That will always be false.)
I'm sure with node there's some fancy way to do that, but the bare-JS way is:
var i,
arr = [{foo: 'bar'}, {foo: 'baz'}],
idx = -1;
for (i = 0; i < arr.length; ++i) {
if (arr[i].foo === 'bar') {
idx = i;
break;
}
}
You could also easily turn that into a function:
function indexOf(arr, pred) {
for (var i = 0; i < arr.length; ++i) {
if (pred(arr)) {
return i;
}
}
return -1;
}
That would give you a lot more verbose usage though (and a bit worse performance), but it might also be a bit more flexible if you find yourself needing to do it often.
console.log(indexOf(arr, function(elem) { return elem.foo === 'bar'; });
.indexOf is returning the correct output; your array doesn't have an element with that string. In fact, it's an array holding two object literals. You don't need .indexOf for objects, instead we must make our own function:
var inObject = function( object, val ) {
for (var i in object) { if ( object.hasOwnProperty(i) ) {
if ( obj[i] === val ) {
return true;
}
}
}
return false;
};
>>> inObject( zio.companies[0], '50bc01938f164ee80b000001' );
: true
Your companies seems to be an array of objects (not ids), which has Id as one of the attributes. indexOf function is used to find the index of the matching element. Since you are passing an ID value to search the index, its not finding it as an element on the array hence returning false.
To fix the problem, you have two options:
Iterate the companies element compare the ID value, if matched return true otherwise false.
Use the object with desired id in as argument in the indexOf function. If value is greater than -1, return true otherwise false.

Count number of object in another object , javascript-json

There seems to have many question asked similar on counting number of element already but I am failing to implement them with mine problem.
After jquery ajax I get JSON data returned which looks something like this
Object {0: Object, 1: Object , xxxx:"asdf" ,yyyy:"asdf", zzzz:"asdf"}
I want to get number of object between this { } braces ( not counting those xxx,yyy element )
I tried .length which doesn't work
I also tried using this Length of a JavaScript object but that return the number of element in each object. I just want the number of object
Thank You
Try this:
var json = { 0: {}, 1: {}, xxxx: "asdf", yyyy: "asdf", zzzz: "asdf" };
function typeOf( obj ) {
return ({}).toString.call( obj )
.match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}
var total = 0;
for ( var o in json ) {
if ( typeOf( json[o] ) == 'object' ) {
total++;
}
}
console.log( total ); //=> 2
Everything is an object in JavaScript. The typeof operator is misleading and won't work in this case. You can use the typeOf function above that I extracted from this blog post: Fixing the JavaScript typeof operator (worth reading). There are other ways of doing it but this seems like the most straightforward.
If it's not just a coincidence that the objects are the ones with numeric property names, and the numeric properties count up sequentially, you could do something like this:
var obj = { /* your object here */ }
for (var i=0; i in obj; i++) {
// use obj[i] for something
}
// i is now equal to the number of numeric properties
This works because as soon as i is high enough that you've run out of properties the in operator will return false. Feel free to use .hasOwnProperty() instead if you prefer.
Obviously this is a specialised solution that doesn't test the type of the different properties at all. (To actually test the type see elclanrs' solution - and either way read the page he linked to.)
Say that the entire json is in a variable called json:
var total_objects = 0;
$.each(json, function () {
if (typeof this == 'object') {
total_objects++;
}
});
However, I am curious as to why you would need to know this.
You can use a customized version from the code of this question Length of Javascript Object (ie. Associative Array) and check for element's type using typeof operator and count only those which are an object (or an array).
Object.sizeObj = function(obj) {
var size = 0, key;
for (key in obj) {
if (typeof key[obj] === 'object' && obj.hasOwnProperty(key)) size++;
}
return size;
};
// Get the count of those elements which are an object
var objectsCount = Object.sizeObj(myArray);

Categories