入门版
function validCommon (arr, ...fields) {let obj = {}return arr.every((item, index) => {return fields.every(field => {if (item[field]) {if (obj[field]) {return obj[field] == item[field]} else {obj[field] = item[field]return true}} else {return true}})})
}
if (!validCommon(arr, 'a', 'b')) {alert('xxx需要保持一致!')return
}
进阶版
function validCommon (arr, ...fields) {let obj = {}for (let i = 0; i < arr.length; i++) {let item = arr[i]for (let j = 0; j < fields.length; j++) {let field = fields[j]if (item[field]) {if (obj[field]) {if (obj[field] != item[field]) {return {i,field,}}} else {obj[field] = item[field]}}}}
}
let valid = validCommon(arr, 'a', 'b')
if (valid) {alert(`第${valid.i}行${valid.field}需要保持一致!`)return
}