检查一个数组的键是否作为其他数组中的值存在

时间:2014-09-17 15:55:31

标签: javascript jquery arrays

情况:

我有两个数组:

  • availablePersons 在这个数组中,我拥有所有可用的人员。 Userid在此处输入。当我使用

    的console.log(availablePersons)

我得到以下内容:

0: 6
1: 7
2: 8
  • 摘要 在这个数组中,我已经选择了所有人,并且选择了他们选择的次数(Key是Userid)的值。 console.log结果如下:

    6:5 7:1

我应该得到什么

我现在需要检查availablePersons中的每个人是否存在于摘要数组中。 因此,检查availablePersons数组的每个值是否作为摘要数组中的键存在。

如果有一个人在摘要数组中不存在,我需要做点什么。

我的代码

for (var i=0; i<availablePersons.length; i++){
  for (var j=0; j<summary.length; j++){
    if(summary[j] !== availablePersons[i]) {
      $('#person-'+i+' .person-sum').text('0');
      $('#person-'+i).fadeIn();
      $('#person-'+i).css('background-color', '#FF0000'); 
      console.log( $('#person-'+i) );
    }
  }
}

问题

console.log输出

选择器:“#person-0”等等,直到选择器:“#person-2”。

但它应该是用户ID(在我的情况下是6,7或8)。

3 个答案:

答案 0 :(得分:0)

要查找availablePersons中不在摘要中的条目,请尝试以下操作:

&#13;
&#13;
// SAMPLE - real data obtained dynamically.
var availablePersons = [6, 7, 8];
var summary = {6: 5, 7: 1};

// For each available person in the list...
for (var i = 0; i < availablePersons.length; i++) {

  // If that person is NOT in the summary data...
  if (typeof summary[availablePersons[i]] === 'undefined') {

    // Do whatever it is you need to do.
    alert('Did not find ' + availablePersons[i]);
  }
}
&#13;
&#13;
&#13;

请注意,availablePersons是一个数组,但summary是一个对象。这样做是取availablePersons中的当前条目,并将其用作summary中的密钥,测试是否已定义此类密钥。

编辑:如果summary是一个数组但您无法更改,请尝试以下操作:

&#13;
&#13;
// SAMPLE - real data obtained dynamically.
var availablePersons = [6, 7, 8];
var summary = [];
summary[6] = 5;
summary[7] = 1;

// Set up a variable we'll use repeatedly.
var foundPerson;

// For each available person in the list...
for (var i = 0; i < availablePersons.length; i++) {
  
  // Note that we have not yet found this person in the summary array.
  foundPerson = false;
  
  // See if they exist in the summary array.
  for (var j = 0; j < summary.length; j++) {
    
    // If this is an empty index in the summary array, skip it.
    if (summary[j] === undefined) {
      continue;
    }
    
    // If this summary entry is the person we were looking for...
    if (typeof summary[availablePersons[i]] !== 'undefined') {
      
      // Make a note that we found them.
      foundPerson = true;
      
      // Stop searching the summary array for this person.
      break;
    }
  }
  
  // If we did NOT find the person in the summary array...
  if ( ! foundPerson) {
    
    // Do whatever it is you need to do.
    alert('Did not find ' + availablePersons[i]);
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

好的,所以你使用第二个数组索引作为第一个数组值的键。 请检查这是否有用:

var availablePersons = [1,5,6];
var summary = Array(10);
summary[5]=10; //Lets say the availablePerson 5 has summary 10 and others did have a summary.
for(i=0;i<availablePersons.length;i++){
    if(summary[availablePersons[i]]==null){
        console.log(availablePersons[i]+" is not there!");
    }
}

答案 2 :(得分:0)

由于availablePerson是一个数组,使用forEach迭代它:

availablePerson.forEach(function(n) {
    // argument n holds the UserId. Doing console.log(n) you see:
    // first step: 6, second step: 7, third step 8
    if (summary[n]) { // check if index[n] is defined ("if UserId is in summary")
       /* here you can do something if User is already there */
    } else { // if UserId is not in summary run your code
        // your i (= index of availablePerson) is changed to n (=UserId);
        $('#person-'+n+' .person-sum').text('0');
        $('#person-'+n).fadeIn();
        $('#person-'+n).css('background-color', '#FF0000'); 
        console.log( $('#person-'+n) );
    }
});