Home > front end >  Javascript: object array, check if item has value
Javascript: object array, check if item has value

Time:02-24

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

CodePudding user response:

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);

CodePudding user response:

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);

CodePudding user response:

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

  • Related