2

I have seen dozens of questions asked for checking if an array is empty. But I can't find the question on how to check if the first item in an array is empty.

Currently, I am doing this check on this way, but I highly doubt that it is the correct way to do this.

var specialtiesListArray = ['', 'not', 'empty', 'value'];

if (specialtiesListArray[0] == '') {
    
    specialtiesListArray.shift(); // <== Removing the first item when empty.

}

console.info({ specialtiesListArray });

The above works fine, but I am wondering is this ok? Are there better ways. And if someone knows a duplicate, then please let me know. I gladly delete the question if it has already been answered.

3
  • pd.isnull(x) or not bool(x) would be pretty good at finding empty values. Commented May 24, 2022 at 17:27
  • 1
    it's good. I don't see any problem with keeping this code as it is, it is simple and clear to understand Commented May 24, 2022 at 17:52
  • 1
    The code is fine as it is, there isn't any noticeable problem. My only piece of advice is to use === in place of ==, in this way if you have empty arrays or zeros it won't delete the element. Remember that in JS [] == '' or 0 == '' are true Commented May 26, 2022 at 7:31

3 Answers 3

2

I think your approach is just fine. You could create a function, and check array length before trying to read element at index 0:

  • utils.js

export const removeFirstItemIfEmpty = (array) => {
    if (array.length > 0 && array[0] == '') {
        array.shift();
    }
    return array;
}

import { removeFirstItemIfEmpty } from './utils.js'

const result = removeFirstItemIfEmpty(['', 'not', 'empty', 'value']);
console.info({ result });
Sign up to request clarification or add additional context in comments.

Comments

1

You could use filter and update the array.

specialtiesListArray = ['', 'not', 'empty', 'value'];
specialtiesListArray = specialtiesListArray.filter((item, index) => index!==0 && item !== '');
console.log(specialtiesListArray);

4 Comments

This would remove not just the first element
this does not remove the empty elements, but it creates a new array (without them) which replaces the original array...
@BrunoPolo - Now it does :)
@GuruprasadJRao if verification is only needed in first element, there is no need to check all items in array (Array.filter will loop through all items)
0

The following function will remove anything that is undefined, null or empty but will allow the number 0 to pass:

var testArr = [['', null, 'not', 'empty', 'value'],[false,2,3,4],[undefined,"", null,"one","two"],[0,1,2,3]];

function ltrimArr(arr){
 while (arr.length && !arr[0]&&arr[0]!==0) arr.shift();
 return arr
}

testArr.forEach(a=>console.log(ltrimArr(a)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.