JavaScript/jQuery 比较数组中的项目

标签 javascript jquery html arrays

我想做的是比较数组中的项目以确定谁首先列出,为简单起见,在我看来,所有人员数据都存储在同一位置。我不知道分离这些数据是否会更容易或更有效。

这是数组,为了便于阅读而缩小了。

var People = [{
    "Name": "Person1",
    "SN": "1",
    "First": "5",
    "Second": "6",
    "Third": "11",
    "Fourth": "7",
    "Fifth": "8",
    "Sixth": "1",
    "Seventh": "10",
    "Eigth": "4",
    "Ninth": "3",
    "Tenth": "2",
    "Eleventh": "13",
    "Twelth": "9",
    "Thirteenth": "12",
    "RDO1": 2,
    "RDO2": 3
}, {
    "Name": "Person2",
    "SN": "2",
    "First": "6",
    "Second": "5",
    "Third": "10",
    "Fourth": "9",
    "Fifth": "7",
    "Sixth": "8",
    "Seventh": "1",
    "Eigth": "4",
    "Ninth": "3",
    "Tenth": "2",
    "Eleventh": "13",
    "Twelth": "11",
    "Thirteenth": "12",
    "RDO1": 2,
    "RDO2": 3
}, {
    "Name": "Person3",
    "SN": "3",
    "First": "6",
    "Second": "9",
    "Third": "7",
    "Fourth": "10",
    "Fifth": "8",
    "Sixth": "5",
    "Seventh": "4",
    "Eigth": "2",
    "Ninth": "3",
    "Tenth": "13",
    "Eleventh": "11",
    "Twelth": "12",
    "Thirteenth": "1",
    "RDO1": 4,
    "RDO2": 5
}];

我尝试了一堆嵌套的 if、for 循环和相互循环的函数,但没有成功。

所以我想要完成的是它会捕获“第一个”选择并与数组中的其他人进行比较,如果其他人有相同的“第一个”选择,它会比较 SN 号以查看哪个是更低。

因此,在上面的数组中,当代码到达 Person3 并且将“6”视为第一选择时,它会查看其他人是否将 6 作为第一选择,并注意到 Person2 也选择了它。然后它会比较 SN 编号,但不会将 6 给 Person3,因为 Person2 的编号较小,因此它会转到 Person3 的第二个编号,并再次进行相同的检查。

我已经为此绞尽脑汁有一段时间了,如果有人能阐明这一点,我将不胜感激。

编辑 - 更多代码

function getPref(index1) {

var sNumb = dsobj[index1].SN;

for (var i = 0; i < dsobj.length; i++) {
if (i != index1) { // Ensure you aren't checking the same person
  if (sNumb > dsobj[i].SN) { //Check to see if the SN is lower than the person you are checking
if (dsobj[index1].First === dsobj[i].First) {
  if (dsobj[index1].Second === dsobj[i].Second) {
    if (dsobj[index1].Third === dsobj[i].Third) {
      if (dsobj[index1].Fourth === dsobj[i].Fourth) {
        if (dsobj[index1].Fifth === dsobj[i].Fifth) {
          if (dsobj[index1].Sixth === dsobj[i].Sixth) {
            if (dsobj[index1].Seventh === dsobj[i].Seventh) {
              if (dsobj[index1].Eigth === dsobj[i].Eigth) {
                if (dsobj[index1].Ninth === dsobj[i].Ninth) {
                  if (dsobj[index1].Tenth === dsobj[i].Tenth) {
                    if (dsobj[index1].Eleventh === dsobj[i].Eleventh) {
                      if (dsobj[index1].Twelth === dsobj[i].Twelth) {
                        if (dsobj[index1].Thirteenth === dsobj[i].Thirteenth) {
                        } else {
                            endChoice = dsobj[index1].Thirteenth;
                        }
                      } else {
                        endChoice = dsobj[index1].Twelth;
                      }
                    } else {
                        endChoice = dsobj[index1].Eleventh;
                    }
                  } else {
                    endChoice = dsobj[index1].Tenth;
                  }
                } else {
                    endChoice = dsobj[index1].Ninth;
                }
              } else {
                endChoice = dsobj[index1].Eigth;
              }
            } else {
                endChoice = dsobj[index1].Seventh;
            }
          } else {
            endChoice = dsobj[index1].Sixth;
          }
        } else {
            endChoice = dsobj[index1].Fifth;
        }
      } else {
        endChoice = dsobj[index1].Fourth;
      }
    } else {
        endChoice = dsobj[index1].Third;
    }
  } else {
    endChoice = dsobj[index1].Second;
  }
} else {
    endChoice = dsobj[index1].First;
}
  } else {
endChoice = dsobj[index1].First;
  }
} else {
    continue;
}
};
return endChoice
}

 function displayPref(index,id,wd) {
endChoice = getPref(index);

if (wd === dsobj[index].RDO1 || wd === dsobj[index].RDO2) {
    $('#'+id+'').append("<td>None</td>");
} else {
    $('#'+id+'').append("<td>"+endChoice+"</td>");
}

}

最佳答案

您可以调整以下功能以满足您的需求。我实现了它,用每个选择和每个选择名称的获胜者填充一个对象:

var winners = {};

function getChoiceWinner(choiceName) {
    for (var i = 0; i < People.length; i++) {
        var choice = People[i][choiceName];
        if(!winners[choiceName]) winners[choiceName] = {};
        winners[choiceName][choice] = People[i];
        for (var j = 0; j < People.length; j++) {
            if (winners[choiceName][choice][choiceName] == People[j][choiceName]) {
                winners[choiceName][choice] = People[j]["SN"] < winners[choiceName][choice]["SN"] ? People[j] : winners[choiceName][choice];
            }
        }
    }
}

fiddle

关于JavaScript/jQuery 比较数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29651346/

相关文章:

javascript - 在 HTML5 和 JavaScript 上为 ubuntu 13.04 编写的小部件

javascript - JS/JQuery : I Can't get the loop right

javascript - 在不离开当前页面的情况下从 Javascript 调用 PHP 脚本

javascript - 在 javascript 中创建表单元素时调用函数

javascript - Javascript 中计数器相同值的 3 倍

javascript - Knockout ObservableArray 不更新 HTML Foreach

javascript - 不透明度过渡适用于淡出,但不适用于淡入

javascript - jQuery 我想将事件类添加到 anchor 标记的链接 ID

html - 如何在相关类 css 中设置最小高度?

html - HTML 页面顶部的警告栏?