这是我的数据
每次用户输入代码时,它都应该是唯一的;如果存在值,它应该显示为错误,例如存在分支代码。
branches: [
{
code: "test",
name: "test",
email: "test",
website: "test.com",
phone: "test",
address: {
street: "test",
city: "test",
township: "test",
state: "test",
zip_code: "test",
country: "test",
},
google_maps_url: "test.com",
},
],
这是我的代码,现在我没有发布完整的数据,忽略了company_details,这更复杂了,在这里我无法显示特定索引字段的错误。索引作为v-on模糊功能的参数。
for(var i=0; i < this.company_details.branches.length; i++){
if( this.company_details.branches[i].code == this.company_details.branches[index].code){
count +=1;
}
if (count==2){
this.BranchCodeArray[index].exists = true;
}
else{
this.BranchCodeArray[index].exists = false;
}
答案 0 :(得分:0)
据我了解,您想输入一些信息,看看输入是否存在于代码字段中?
您可以使用过滤器获取输入匹配的项数,如果结果数组的长度> 0,则输入不是唯一的。
const branches= [
{
code: 'test',
name: 'test',
email: 'test',
website: 'test.com',
phone: 'test',
address: {
street: 'test',
city: 'test',
township: 'test',
state: 'test',
zip_code: 'test',
country: 'test'
},
google_maps_url: 'test.com'
}
];
const doesExist = 'code';
const doesNotExist = 'does not exist'
const exists = !!branches.filter((branch) => branch.code === doesExist).length
const notExists = !!branches.filter((branch) => branch.code === doesNotExist).length
您还可以映射数据以生成平面代码。然后,您可以查看codes数组是否包含您的输入数据。
const branches= [
{
code: 'test',
name: 'test',
email: 'test',
website: 'test.com',
phone: 'test',
address: {
street: 'test',
city: 'test',
township: 'test',
state: 'test',
zip_code: 'test',
country: 'test'
},
google_maps_url: 'test.com'
}
];
const doesExist = 'code';
const doesNotExist = 'does not exist'
const codes = branches.map((branch) => branch.code);
if(codes.includes(doesExist)){
// Your error logic
}
codes.includes(doesNotExist)