0

given that I have the following script, how can I only count if the array item has the value defined, in the following scenario i get 3 because it counts the arrays, but how do I check if the actual array attribute of firstName or lastname is not empty, I want it to count if either first name or last name is populated.

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}] var noGuests = 0 for (a in x) { noGuests++ } alert(noGuests) 

update My application's javascript render engine is outdated and can't use filter's.

SpiderMonkey 1.8.5

2
  • 1
    There's no multidimensional array in your question (a nitpick: JS does't even have a concept of multidimensional arrays). See also stackoverflow.com/q/9329446/1169519
    – Teemu
    CommentedFeb 24, 2022 at 6:26
  • 1
    AFAIK SpiderMonkey 1.8.5 is the first full version of ES5, that should contain filter (which was added to SpiderMonkey 1.6).
    – Teemu
    CommentedFeb 28, 2022 at 6:36

3 Answers 3

2

You can just filter the array by item which item.firstName and item.lastName is not empty and get the length of the result array

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}] const result = x.filter(item => item.firstName && item.lastName).length; console.log(result);

    1

    If any of the firstName or lastName is populated then this will count. If you need both firstName and lastName then change the condition from || to &&.

    var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}] var noGuests = 0 for (a in x) { if (x[a]['firstName'] || x[a]['lastName']) { noGuests++ } } console.log(noGuests); 

    Or can do so using forEach loop

    var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}] var noGuests = 0 x.forEach(x => { if (x.firstName || x.lastName) noGuests++; }) console.log(noGuests); 
    8
    • That's it! thank you for saving me time.CommentedFeb 24, 2022 at 6:26
    • You can also use forEach. I'll modify the answer with another solution.CommentedFeb 24, 2022 at 6:26
    • 1
      Don't use for..in to iterate arrays.
      – Teemu
      CommentedFeb 24, 2022 at 6:32
    • why @Teemu performance issues? or somethingCommentedFeb 24, 2022 at 6:34
    • 1
      you can use pop method to remove from the array.CommentedFeb 25, 2022 at 7:12
    1
    function count_non_empty(collection) { return filter_non_empty(collection).length; } function filter_non_empty(collection) { return collection && collection.length && collection.filter(elem => (elem.firstName || '').trim() // <--- If firstName is empty or empty-like string || (elem.lastName || '').trim() // <--- If lastName is empty or empty-like string ) || []; } 

    Illustration

    function count_non_empty(collection) { return filter_non_empty(collection).length; } function filter_non_empty(collection) { return collection && collection.length && collection.filter(elem => (elem.firstName || '').trim() // <--- If firstName is empty or empty-like string || (elem.lastName || '').trim() // <--- If lastName is empty or empty-like string ) || []; } let onlyLastName = { "firstName": "", "lastName": "SomeLastName" }; let onlyFirstName = { "firstName": "SomeFirstName", "lastName": "" }; let emptyLookingFirstName = { "firstName": " ", "lastName": "SomeLastName" }; let emptyLookingLastName = { "firstName": " SomeFIrstName ", "lastName": " " }; let havingBoth = { "firstName": "guest1fnamee", "lastName": "guest1fnamee" }; let notHavingAny = { "firstName": " ", "lastName": "" } let x = [{ "firstName": "guest1fnamee", "lastName": "guest1fnamee" }, { "firstName": "guest2fnamee", "lastName": "guest2lnamee" }, { "firstName": " ", "lastName": "" } ]; console.log("Count - x:", count_non_empty(x)); console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth])); console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth, emptyLookingFirstName])); console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth, emptyLookingLastName])); console.log("Count - Not Having Any:", count_non_empty([notHavingAny])); console.log("Count - Having Both:", count_non_empty([havingBoth])); console.log("Count - Not Having Any with Having Both:", count_non_empty([notHavingAny, havingBoth])); console.log("Count - Not Having Any with Empty Looking First Name:", count_non_empty([notHavingAny, emptyLookingFirstName])); console.log("Count - Not Having Any with Empty Looking Last Name:", count_non_empty([notHavingAny, emptyLookingLastName]));


    WYSIWYG => WHAT YOU SHOW IS WHAT YOU GET

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.