Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何验证给定的输入是否存在于json数组中?_Javascript_Vue.js - Fatal编程技术网

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

Javascript 如何验证给定的输入是否存在于json数组中?,javascript,vue.js,Javascript,Vue.js,这是我的数据 每次用户输入代码时,它都应该是唯一的,如果存在值,它应该显示为错误,就像分支代码存在一样 branches: [ { code: "test", name: "test", email: "test", website: "test.com", phone: "test", address: {

这是我的数据

每次用户输入代码时,它都应该是唯一的,如果存在值,它应该显示为错误,就像分支代码存在一样

 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",
          },
        ],
这是我的代码忽略公司详细信息,因为现在我没有发布完整的数据,这更复杂,因为我无法显示特定索引字段的错误。索引作为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;
        }
for(var i=0;i
据我所知,您想获取一些输入,看看代码字段中是否存在输入

您可以使用筛选器获取输入匹配的项数,如果结果数组的长度>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
您还可以映射数据以生成一个平面代码数组。然后,您可以查看代码数组是否包含您的输入数据

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)

据我所知,您希望获取一些输入,并查看代码字段中是否存在输入

您可以使用筛选器获取输入匹配的项数,如果结果数组的长度>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
您还可以映射数据以生成一个平面代码数组。然后,您可以查看代码数组是否包含您的输入数据

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)

你自己尝试了什么?代码你添加了我更新时已经拥有的代码。你自己尝试了什么?代码你添加了我更新时已经拥有的代码。属性“代码”就像只存储唯一的值,如果输入的代码值已经存在,我应该向用户显示错误。就像已经有一个数据作为“test”,所以当用户使用与“test”相同的值输入其他数组时,我应该以某种方式显示错误消息。@NikilJ我的解决方案就是这样做的?我如何在这里传递输入值?@tom基本上我应该将输入值作为参数传递给这个函数(codes.includes('test'){//您的错误逻辑警报(“hello world”);}//我已经尝试过了,但是当值输入到“test”中时已经存在,任何解决方案,如果它有2个测试,那么我可以显示错误。属性“code”类似于只存储唯一的值,如果输入的代码值已经存在,我应该向用户显示错误。类似于已经存在一个数据作为“test”,所以当用户输入与“test”值相同的其他数组时,我应该以某种方式显示错误消息。@NikilJ我的解决方案就是这样做的?我如何在这里传递输入值?@tom基本上我应该将输入值作为参数传递给这个函数(codes.includes('test'){//您的错误逻辑警报(“hello world”);}//我已经尝试过了,但是当值输入到“test”中时已经有,任何解决方案,所以如果它有2个测试,所以我可以显示错误。