Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.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 在连接另一个变量时初始化变量_Javascript_Jquery - Fatal编程技术网

Javascript 在连接另一个变量时初始化变量

Javascript 在连接另一个变量时初始化变量,javascript,jquery,Javascript,Jquery,我使用变量value\u from\u db for (var i = 0; i < arrayLength; i++) { var value_from_db = array[i].value; } 目的是 for (var i = 0; i < arrayLength; i++) { if( array[i].condition == '0' ) { jQuery(

我使用变量
value\u from\u db

for (var i = 0; i < arrayLength; i++) {

   var value_from_db = array[i].value;
}
目的是

  for (var i = 0; i < arrayLength; i++) {
     if( array[i].condition == '0' ) {                                  

           jQuery("#username").on('change', function() {

            var value = this.value;         

            if( value === array[i].value ) {  //because array[i].value doesnot seem to work here
                count++;
                alert(count);
            } 
     }
}
for(变量i=0;i

因为数组[i].value似乎没有通过匿名函数,所以我试图区分变量。这里有几个选项。第一个明显的选项是使用数组:

var value_from_db = array.map(item => item.value)
// then access them with the index directly
console.log(value_from_db[0])
但是,如果您真的想要这些变量名,您可以很容易地将它们放在一个对象中:

var myObj = {}
array.forEach((item, i) => myObj[`value_from_db_${i}`] = item.value)
// the use with something like
console.log(myObj.value_from_db_0)

在阅读了@Igors closure answer上的注释后,看起来您想要做的是(这是从模糊的注释中猜测出来的最佳结果),观察
#username
的更改,并找出是否所有数组值都在某个点匹配。如果我是正确的,您可以这样做:

// remove unwanted condition items
var conds = array.filter(item => item.condition === '0')
// watch for element changes
jQuery("#username").on('change', function() {
  var comparer = this.value
  // update a match key for any items where a match is found
  conds
    .filter(item => item.value === comparer)
    .forEach(item => item.match = true)

  // check if all matches have been found
  if(conds.every(item => item.match)){
    alert('all matched')
  }
})

这里有几个选项。第一个明显的选项是使用数组:

var value_from_db = array.map(item => item.value)
// then access them with the index directly
console.log(value_from_db[0])
但是,如果您真的想要这些变量名,您可以很容易地将它们放在一个对象中:

var myObj = {}
array.forEach((item, i) => myObj[`value_from_db_${i}`] = item.value)
// the use with something like
console.log(myObj.value_from_db_0)

在阅读了@Igors closure answer上的注释后,看起来您想要做的是(这是从模糊的注释中猜测出来的最佳结果),观察
#username
的更改,并找出是否所有数组值都在某个点匹配。如果我是正确的,您可以这样做:

// remove unwanted condition items
var conds = array.filter(item => item.condition === '0')
// watch for element changes
jQuery("#username").on('change', function() {
  var comparer = this.value
  // update a match key for any items where a match is found
  conds
    .filter(item => item.value === comparer)
    .forEach(item => item.match = true)

  // check if all matches have been found
  if(conds.every(item => item.match)){
    alert('all matched')
  }
})

在编辑问题之后,这就变成了一个完全不同的问题,与闭包和捕获的变量有关

可能重复调用
jQuery(#username”)。在('change')上,…
看起来可疑,但可能只有一个
array[i]。数组中的条件==“0”

for (var i = 0; i < arrayLength; i++) {
  if( array[i].condition == '0' ) {                                  
    jQuery("#username").on('change', (function(aValue) {
      return function() {
        var value = this.value;         
        if (value === aValue) {
          count++;
          alert(count);
        }
      }; 
    })(array[i].value));
  }
}
for(变量i=0;i
编辑问题后,这就变成了一个与闭包和捕获变量有关的完全不同的问题

可能重复调用
jQuery(#username”)。在('change')上,…
看起来可疑,但可能只有一个
array[i]。数组中的条件==“0”

for (var i = 0; i < arrayLength; i++) {
  if( array[i].condition == '0' ) {                                  
    jQuery("#username").on('change', (function(aValue) {
      return function() {
        var value = this.value;         
        if (value === aValue) {
          count++;
          alert(count);
        }
      }; 
    })(array[i].value));
  }
}
for(变量i=0;i
你应该创建一个数组,并将你的值添加到其中。你打算如何使用这些变量?@igor我已经编辑了这个问题,请看一看。哦,这与闭包和捕获的变量完全不同。有多少个元素具有
id=“username”
页面上有吗?您应该创建一个数组并将值添加到其中。您打算如何使用这些变量?@igor我已经编辑了这个问题,请看一看。哦,这与闭包和捕获的变量完全不同。有多少元素具有
id=“username”
页面上有吗?谢谢。但是计数似乎随着每次更改而增加。我需要检查的是是否所有条件都在里面,以便通过,所以我想匹配
排列长度
计数
来知道。但是这似乎不起作用。你能指导我吗。@micky我会的,只是我不知道有什么问题哟你正在尝试解决。我想知道条件是否在每次迭代中都会通过。谢谢。但每次更改后计数似乎都在增加。我需要检查的是,是否所有条件都在通过,所以我想匹配
排列长度
计数
,以了解情况。但是这似乎不起作用。你能指导我吗。@mi我会的,只是我不知道你们想解决什么问题。我想知道条件是否经过每次迭代。