JavaScript / jQuery比较数组中的项目

时间:2015-04-15 13:17:14

标签: 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循环,以及相互循环的功能但没有成功。

所以我想要完成的是它会抓住第一个"选择并与数组中的其他人进行比较,如果其他人拥有相同的" First"选择,它会比较SN编号以查看较低者。

所以在上面的数组中代码来到Person3时它会看到" 6"作为第一选择,它看看是否有其他人有6作为他们的第一选择和通知Person2也选择它。然后它比较SN号并且不给Person3 6,因为Person2的数字较小,所以它继续移动到Person3的Second并且再次检查相同。

我现在已经把头撞了一会儿,如果有人能说清楚,我会非常感激。

修改 - 更多代码

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>");
}

}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

在做了一些你需要的工作后我得到了一个解决方案......我想。

输出有点混乱,但由于你的数组会更大,你会得到按First然后按SN分组的所有内容

function groupBy( array , f )
{
  var groups = {};
  array.forEach( function( o )
  {
    var group = JSON.stringify( f(o) );
    groups[group] = groups[group] || [];
    groups[group].push( o );  
  });
  return Object.keys(groups).map( function( group )
  {
    return groups[group]; 
  })
}

var groupedByFirst = groupBy(People, function(item)
{
  return [item.First];
});

var finalGrouping = new Array;
 for (i = 0; i <= groupedByFirst.length-1; i++) {
     var test = groupBy(groupedByFirst[i], function(item) {
        return [item.SN]
    });
    finalGrouping.push(test);
}
console.log(finalGrouping)

这是小提琴,所以你可以测试它:

Fiddle

这个和最后一个答案之间的区别在于这是动态的。所以基本上你可以继续进入数组并对不同的属性进行排序。

此外,如果您想要更改属性,它的维护方式也会更加可维护。

因此,对于您需要的东西,列出属性..只需按顺序打印数组,然后按First列出所有内容,然后按SN

列出