简体   繁体   English

如何验证给定的输入是否存在于 json 数组中?

[英]How to validate whether the given input exists in json array or not?

this is my data这是我的数据

every time user enters code it should be unique, if there is exists value it should show as error like branch code is exists.每次用户输入代码时它应该是唯一的,如果存在值它应该显示为错误,例如分支代码存在。

 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",
          },
        ],

this is my code ignore company_details for now i didnt post full data, this is more complex where i cannot display error for particular index field.这是我的代码忽略 company_details 现在我没有发布完整数据,这更复杂,我无法显示特定索引字段的错误。 the index come as parameter to the function on v-on blur.该索引作为 v-on 模糊的 function 的参数。

 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;
        }

From what I understand you want to take some input and see if the input exists in the code field?据我了解,您想获取一些输入并查看输入是否存在于代码字段中?

You can use filter to get the number of items that the input matches, if the length of the resulting array is > 0, the input is not unique.您可以使用过滤器获取输入匹配的项目数,如果结果数组的长度> 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

You could also map over your data to produce a flat array of codes.您还可以对数据进行 map 以生成平面代码数组。 You can then see if the codes array includes your input data.然后您可以查看代码数组是否包含您的输入数据。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM