[Vuejs]-How to iterate over object values and condtionally fire an event if all of them are filled?

2👍

Adding to what @user1538301 answered, if you want to know which properties fail to fulfill the criteria is possible to use Array::filter the following way:

const missingProperties = Object.values(this.currentObjectData)
    .filter(data => objectData.report_type);

if(missingProperties.length > 1) {
    // Do something with the missing properties
} else {
    EventBus.$emit("allObjectsAreValidated");
}

Also a few things I noticed in the code:

  1. this.objectData : I’m assuming your intention is not to use a variable from the object but rather the parameter of the anonymous function, so objectData would be the right variable.

  2. !this.objectData.report_type || this.objectData.report_type === "" : An empty string is a falsy value so when evaluated as bool is equivalent to false. (More info: FreeCodeCamp)

2👍

Use Array.prototype.every

Try:

const allObjectsMatch = Object.values(this.currentObjectData).every(
    value => /*return a boolean if they value matches*/
);
allObjectsMatch && EventBus.$emit("allObjectsAreValidated");

Leave a comment