Updated fix to: let isValidMunicipalities = true for (const province in calendargroup) { const municipalities = calendargroup[province].map(item => item.municipality) const uniqueMunicipalities = new Set(municipalities) /* Each municipality should have 2 cropping calendar rows (data1, data2) */ const notEqual = uniqueMunicipalities.size * 2 !== municipalities.length if (notEqual) { isValidMunicipalities = false break } } /* Throw error to avoid uploading/processing data with errors further */ if (!isValidMunicipalities) { throw new Error('Missing data1 or data2 municipality row') }