我有一个对象数组,我需要执行任务。
在不使用sort()
方法的情况下,我需要根据投票给特朗普的人以及投票给希拉里的人来对数组内的对象进行排序。
此外,我需要找到投票给特朗普最多的五个州,并找到投票给希拉里最多的五个州。
这是数组:
var states = [{
stateName: "Delaware",
votesForHillary: 14,
votesForTrump: 123,
population: 925749
}, {
stateName: "Pennsylvania",
votesForHillary: 240,
votesForTrump: 1,
population: 12773801
}, {
stateName: "New Jersey",
votesForHillary: 124,
votesForTrump: 15,
population: 8899339
}, {
stateName: "Georgia",
votesForHillary: 12,
votesForTrump: 353,
population: 9992167
}, {
stateName: "Connecticut",
votesForHillary: 23,
votesForTrump: 135,
population: 3596080
}, {
stateName: "Massachusetts",
votesForHillary: 50,
votesForTrump: 53,
population: 6692824
}, {
stateName: "Maryland",
votesForHillary: 424,
votesForTrump: 23,
population: 600000
}, {
stateName: "South Carolina",
votesForHillary: 0,
votesForTrump: 1,
population: 4774839
}, {
stateName: "New Hampshire",
votesForHillary: 34,
votesForTrump: 14,
population: 1323459
}, {
stateName: "Virginia",
votesForHillary: 233,
votesForTrump: 153,
population: 8260405
}, {
stateName: "New York",
votesForHillary: 253,
votesForTrump: 15,
population: 19651127
}, {
stateName: "North Carolina",
votesForHillary: 5,
votesForTrump: 404,
population: 600000
}, {
stateName: "Rhode Island",
votesForHillary: 4,
votesForTrump: 6,
population: 1051511
}, {
stateName: "Vermont",
votesForHillary: 23,
votesForTrump: 54,
population: 626630
}, {
stateName: "Kentucky",
votesForHillary: 46,
votesForTrump: 647,
population: 4395295
}, {
stateName: "Tennessee",
votesForHillary: 44,
votesForTrump: 600,
population: 6495978
}, {
stateName: "Ohio",
votesForHillary: 35,
votesForTrump: 45,
population: 11570808
}, {
stateName: "Louisiana",
votesForHillary: 65,
votesForTrump: 56,
population: 4625470
}, {
stateName: "Indiana",
votesForHillary: 21,
votesForTrump: 12,
population: 6570902
}, {
stateName: "Mississippi",
votesForHillary: 340,
votesForTrump: 151,
population: 2991207
}, {
stateName: "Illinois",
votesForHillary: 114,
votesForTrump: 53,
population: 12882135
}, {
stateName: "Alabama",
votesForHillary: 35,
votesForTrump: 1351,
population: 4833722
}, {
stateName: "Maine",
votesForHillary: 1,
votesForTrump: 2,
population: 1328302
}, {
stateName: "Missouri",
votesForHillary: 240,
votesForTrump: 15,
population: 6044171
}, {
stateName: "Arkansas",
votesForHillary: 53,
votesForTrump: 153,
population: 2959373
}, {
stateName: "Michigan",
votesForHillary: 35,
votesForTrump: 647,
population: 600000
}, {
stateName: "Florida",
votesForHillary: 46,
votesForTrump: 474,
population: 19552860
}, {
stateName: "Texas",
votesForHillary: 0,
votesForTrump: 0,
population: 26448193
}, {
stateName: "Iowa",
votesForHillary: 436,
votesForTrump: 123,
population: 3090416
}, ];
这是我使用sort()方法完成它的方式。
states.sort(function (a, b) {
return b.votesForTrump - a.votesForTrump;
});

这不是家庭作业,这是我想要了解的挑战。
答案 0 :(得分:0)
下次,请在寻求帮助之前先展示一些自己的努力;
对于您的问题,需要map
,filter
,Math.max
的组合;
var states = [{
stateName: "Delaware",
votesForHillary: 14,
votesForTrump: 123,
population: 925749
}, {
stateName: "Pennsylvania",
votesForHillary: 240,
votesForTrump: 1,
population: 12773801
}, {
stateName: "New Jersey",
votesForHillary: 124,
votesForTrump: 15,
population: 8899339
}, {
stateName: "Georgia",
votesForHillary: 12,
votesForTrump: 353,
population: 9992167
}, {
stateName: "Connecticut",
votesForHillary: 23,
votesForTrump: 135,
population: 3596080
}, {
stateName: "Massachusetts",
votesForHillary: 50,
votesForTrump: 53,
population: 6692824
}, {
stateName: "Maryland",
votesForHillary: 424,
votesForTrump: 23,
population: 600000
}, {
stateName: "South Carolina",
votesForHillary: 0,
votesForTrump: 1,
population: 4774839
}, {
stateName: "New Hampshire",
votesForHillary: 34,
votesForTrump: 14,
population: 1323459
}, {
stateName: "Virginia",
votesForHillary: 233,
votesForTrump: 153,
population: 8260405
}, {
stateName: "New York",
votesForHillary: 253,
votesForTrump: 15,
population: 19651127
}, {
stateName: "North Carolina",
votesForHillary: 5,
votesForTrump: 404,
population: 600000
}, {
stateName: "Rhode Island",
votesForHillary: 4,
votesForTrump: 6,
population: 1051511
}, {
stateName: "Vermont",
votesForHillary: 23,
votesForTrump: 54,
population: 626630
}, {
stateName: "Kentucky",
votesForHillary: 46,
votesForTrump: 647,
population: 4395295
}, {
stateName: "Tennessee",
votesForHillary: 44,
votesForTrump: 600,
population: 6495978
}, {
stateName: "Ohio",
votesForHillary: 35,
votesForTrump: 45,
population: 11570808
}, {
stateName: "Louisiana",
votesForHillary: 65,
votesForTrump: 56,
population: 4625470
}, {
stateName: "Indiana",
votesForHillary: 21,
votesForTrump: 12,
population: 6570902
}, {
stateName: "Mississippi",
votesForHillary: 340,
votesForTrump: 151,
population: 2991207
}, {
stateName: "Illinois",
votesForHillary: 114,
votesForTrump: 53,
population: 12882135
}, {
stateName: "Alabama",
votesForHillary: 35,
votesForTrump: 1351,
population: 4833722
}, {
stateName: "Maine",
votesForHillary: 1,
votesForTrump: 2,
population: 1328302
}, {
stateName: "Missouri",
votesForHillary: 240,
votesForTrump: 15,
population: 6044171
}, {
stateName: "Arkansas",
votesForHillary: 53,
votesForTrump: 153,
population: 2959373
}, {
stateName: "Michigan",
votesForHillary: 35,
votesForTrump: 647,
population: 600000
}, {
stateName: "Florida",
votesForHillary: 46,
votesForTrump: 474,
population: 19552860
}, {
stateName: "Texas ",
votesForHillary: 0,
votesForTrump: 0,
population: 26448193
}, {
stateName: "Iowa",
votesForHillary: 436,
votesForTrump: 123,
population: 3090416
}, ];
let trumpStates = states.filter((data) => {return data["votesForTrump"] > data["votesForHillary"]});
let hillaryStates = states.filter((data) => {return data["votesForTrump"] < data["votesForHillary"]});
let allTrumpVotes = [];
let allHillaryVotes = [];
trumpStates.map((data) => allTrumpVotes.push(data["votesForTrump"]));
hillaryStates.map((data) => allHillaryVotes.push(data["votesForHillary"]));
let maxTrumpVotes = Math.max(...allTrumpVotes);
let maxHillaryVotes = Math.max(...allHillaryVotes);
console.log(allHillaryVotes)
let maxTrumpState = trumpStates.filter((data) => {
if(data["votesForTrump"] == maxTrumpVotes) return data;
});
let maxHillaryState = hillaryStates.filter((data) => {
if(data["votesForHillary"] == maxHillaryVotes) return data;
});
console.log(maxHillaryState,maxTrumpState)
答案 1 :(得分:0)
下面的功能将有助于整理东西,你也可以做同样的排序,以获得希拉里的大多数选票状态。
function sorting() {
console.log("Lenght : " + states.length);
let TopVotedToTrump: typeof states;
let TopVotedToHillary: typeof states;
TopVotedToTrump.push(states[0]);
for (let i: number = 0; i < states.length-1 ; i++) {
for (let j: number = i + 1; j < states.length; j++) {
if ((states[i].votesForTrump < states[j].votesForTrump) || (states[i].votesForTrump == states[j].votesForTrump && states[i].votesForHillary < states[j].votesForHillary))
{
console.log(states[i].votesForTrump + " swapped with " + states[j].votesForTrump);
let temp = states[i];
states[i] = states[j]
states[j] = temp
}
if ((states[i].votesForHillary < states[j].votesForHillary)) {
let temp = states[i];
states[i] = states[j]
states[j] = temp
}
}
if (TopVotedToTrump.indexOf(states[i]) != -1) {
TopVotedToTrump.push(states[i]);
}
}
//for (let i of states) {
// console.log("{" + " State: " + i.stateName + " Population: " + i.population + " Votes For Hillary: " + i.votesForHillary + " Votes For Trump: "+ i.votesForTrump + "}");
//}
//console.log("Result : " + JSON.parse(states.toString()));
}