情况:
我有两个数组:
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)。
答案 0 :(得分:0)
要查找availablePersons
中不在摘要中的条目,请尝试以下操作:
// 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;
请注意,availablePersons
是一个数组,但summary
是一个对象。这样做是取availablePersons
中的当前条目,并将其用作summary
中的密钥,测试是否已定义此类密钥。
编辑:如果summary
是一个数组但您无法更改,请尝试以下操作:
// 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;
答案 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) );
}
});