类对象数组的验证规则



我一直有一个问题。我要做的是为数据添加一些验证规则。数据示例如下:

const data = [
{ name: 'Benz', class: 'Car', grade: 'A', isDefault: true },
{ name: 'BMW', class: 'Car', grade: 'B', isDefault: true },
{ name: 'Ford', class: 'Car', grade: 'C', isDefault: true },
{ name: 'Hyundai', class: 'Car', grade: 'C', isDefault: true },
{ name: 'Kia', class: 'Car', grade: 'C', isDefault: true },
{ name: 'Mustang', class: 'Car', grade: 'D', isDefault: true },
{ name: 'Zeep', class: 'Car', grade: 'E', isDefault: true },
{ name: 'Andy', class: 'Boat', grade: 'A', isDefault: true },
{ name: 'Tommy', class: 'Boat', grade: 'B', isDefault: true },
{ name: 'Judy', class: 'Boat', grade: 'C', isDefault: true },
{ name: 'John', class: 'Boat', grade: 'D', isDefault: true },
{ name: 'James', class: 'Boat', grade: 'E', isDefault: false },
{ name: 'Romio', class: 'Boat', grade: 'F', isDefault: false },
{ name: 'Michael', class: 'Boat', grade: 'F', isDefault: false },
{ name: 'Ron', class: 'Boat', grade: 'F', isDefault: false },
]

[验证规则]

如果'class'和'grande'相同,它必须有一个默认值(isDefault = true),但默认值不能超过1

也许很难理解这个规则。以现代汽车为例。现代汽车可以默认等级为"Car"和等级为"C"。但在"Car"类中还有另一个默认值"Ford"one_answers"Kia"。"C"级。因此,验证的结果是失败。

我们设class 'Boat' &&F级案件。没有分配这种情况的默认值。因此,验证的结果是失败。

提前谢谢你。

下面是实现目标的一种可能的方法。

<<p>

代码片段/strong>

// method to validate given class-grade combination
const myValidate = (pClass, pGrade, arr) => {
// first determine if any data matches the class-grade combination
const filterData = arr.filter(
obj => (
obj.class === pClass &&
obj.grade === pGrade
)
);
// if yes (ie, class-grade combination exists),
if (filterData.length > 0) {
// count how many "isDefault" with value "true" exists
const defaultTrueCount = filterData.filter(({ isDefault }) => !!isDefault).length;
// if exactly 1 "isDefault" with value "true", then test "pass"-ed
// else, "fail"-ed
if (defaultTrueCount === 1) return 'Pass'
else return 'Fail';
} else {
// no data matched - so case is neither pass nor fail
return `class: ${pClass} and grade: ${pGrade} combination not found in data`;
}
};
const data = [
{ name: 'Benz', class: 'Car', grade: 'A', isDefault: true },
{ name: 'BMW', class: 'Car', grade: 'B', isDefault: true },
{ name: 'Ford', class: 'Car', grade: 'C', isDefault: true },
{ name: 'Hyundai', class: 'Car', grade: 'C', isDefault: true },
{ name: 'Kia', class: 'Car', grade: 'C', isDefault: true },
{ name: 'Mustang', class: 'Car', grade: 'D', isDefault: true },
{ name: 'Zeep', class: 'Car', grade: 'E', isDefault: true },
{ name: 'Andy', class: 'Boat', grade: 'A', isDefault: true },
{ name: 'Tommy', class: 'Boat', grade: 'B', isDefault: true },
{ name: 'Judy', class: 'Boat', grade: 'C', isDefault: true },
{ name: 'John', class: 'Boat', grade: 'D', isDefault: true },
{ name: 'James', class: 'Boat', grade: 'E', isDefault: false },
{ name: 'Romio', class: 'Boat', grade: 'F', isDefault: false },
{ name: 'Michael', class: 'Boat', grade: 'F', isDefault: false },
{ name: 'Ron', class: 'Boat', grade: 'F', isDefault: false },
];
console.log('validate class: Car, grade: C | result: ', myValidate('Car', 'C', data));
console.log('validate class: Boat, grade: F | result: ', myValidate('Boat', 'F', data));
console.log('validate class: Car, grade: A | result: ', myValidate('Car', 'A', data));
.as-console-wrapper { max-height: 100% !important; top: 0 }

在上面的代码片段中添加内联注释。

你是这个意思吗?(类首字母== grape =>isDefault:true;类首字母!= grape =>isDefault:false)

const data = [{ name: 'Benz', class: 'Car', grade: 'A', isDefault: true },{ name: 'BMW', class: 'Car', grade: 'B', isDefault: true },{ name: 'Ford', class: 'Car', grade: 'C', isDefault: true },{ name: 'Hyundai', class: 'Car', grade: 'C', isDefault: true },{ name: 'Kia', class: 'Car', grade: 'C', isDefault: true },{ name: 'Mustang', class: 'Car', grade: 'D', isDefault: true },{ name: 'Zeep', class: 'Car', grade: 'E', isDefault: true },{ name: 'Andy', class: 'Boat', grade: 'A', isDefault: true },{ name: 'Tommy', class: 'Boat', grade: 'B', isDefault: true },{ name: 'Judy', class: 'Boat', grade: 'C', isDefault: true },{ name: 'John', class: 'Boat', grade: 'D', isDefault: true },{ name: 'James', class: 'Boat', grade: 'E', isDefault: false }, { name: 'Romio', class: 'Boat', grade: 'F', isDefault: false },{ name: 'Michael', class: 'Boat', grade: 'F', isDefault: false },{ name: 'Ron', class: 'Boat', grade: 'F', isDefault: false }]
for(let elem of data) {elem.isDefault=(elem.class[0]==elem.grade)}
console.log(data)

相关内容

  • 没有找到相关文章

最新更新