I have a set of data for 5 people. The data goes: "#ofKeys","PreferedCrate","Round1Results","Round2Results","Round2Results"
我有一组5个人的数据。数据:“# ofKeys”、“PreferedCrate”、“Round1Results”,“Round2Results”、“Round2Results”
What I would like is for the data to be in some form of a 2D array so that it covers all information about all 5 players.
我想要的是数据以二维数组的形式出现这样它就能涵盖所有5个玩家的信息。
I will then like for the data to log the average of the 3 rounds for each player, as well as best and worst result that each player got.
然后我希望数据记录下每个玩家3轮的平均成绩,以及每个玩家得到的最好和最差的成绩。
I am not really sure how to approach this, so any help would be appreciated.
我真的不知道该怎么做,所以我很感激你的帮助。
So far all I have really done is made a little array of items as follows:
到目前为止,我所做的只是做了一些项目的数组如下:
var playerdata = [[2, Assault, 1,1,10],
[0, Support, 3,2,11],
[1, Assault, 7,12,1],
[0, Assault, 5,9,14],
[0, Assault, 11,17,18]];
3 个解决方案
#1
1
You can map your playerdata
array and calculate the average/best/worst for each player:
您可以映射您的playerdata数组并计算每个播放器的平均/最佳/最差值:
var playerdata = [[2, 'Assault', 1,1,10],
[0, 'Support', 3,2,11],
[1, 'Assault', 7,12,1],
[0, 'Assault', 5,9,14],
[0, 'Assault', 11,17,18]];
var calculated = playerdata.map((player) => {
const rounds = player.slice(2);
return {
player,
average: average(rounds),
best: Math.max(...rounds),
worst: Math.min(...rounds)
};
});
function average(numbers) {
return numbers.reduce((a, b) => a + b, 0) / numbers.length;
}
console.log(calculated);
console.log('Player 1 average', calculated[0].average);
#2
2
While you asked about more round results, you could treat the array from index 2 as data for the rounds.
当您询问更全面的结果时,您可以将索引2中的数组作为轮的数据。
var playerdata = [[2, 'Assault', 1, 1, 10], [0, 'Support', 3, 2, 11], [1, 'Assault', 7, 12, 1], [0, 'Assault', 5, 9, 14], [0, 'Assault', 11, 17, 18]],
result = playerdata.map(player => {
const data = player.slice(2);
return {
player,
average: data.reduce((a, b) => a + b) / data.length,
best: Math.max(...data),
worst: Math.min(...data)
};
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#3
1
What makes the most sense (to me) is to map the data into objects for each player, like so:
(对我来说)最有意义的是将数据映射到每个玩家的对象中,比如:
This solution makes use of:
本解决方案利用:
Array.prototype.map()
(For mapping into array of objects)
array .prototype.map()(用于映射到对象数组)
Array.prototype.reduce()
(For summing the rounds data generically)
reduce()(用于一般地求和round数据)
ES6 Spread Syntax (For gathering/spreading rounds data)
ES6扩展语法(用于收集/扩展回合数据)
ES6 Destructuring Assignment (For easily extracting player data into local variables)
ES6破坏分配(便于将玩家数据提取到局部变量中)
ES6 Find (For plucking a single object out of an array by property value)
ES6 Find(用于按属性值从数组中提取单个对象)
var playerdata = [[2, "Assault", 1,1,10],
[0, "Support", 3,2,11],
[1, "Assault", 7,12,1],
[0, "Assault", 5,9,14],
[0, "Assault", 11,17,18]];
var mapped = playerdata.map(function (arr, index) {
// Extract player data from array through Destructuring
// Same as var NumberOfKeys = arr[0], PreferredCrate = arr[1], etc.
// gather the rest of the arr elements as rounds data
var [NumberOfKeys, PreferedCrate, ...rounds] = arr;
// return an object with the data we want back
return {
PlayerName: "Player " + (index + 1),
NumberOfKeys,
PreferedCrate,
NumberOfRounds: rounds.length,
BestResult: Math.max(...rounds),
WorstResult: Math.min(...rounds),
Average: rounds.reduce((total, round) => total + round) / 3
};
});
function getPlayerAverage(playerName, data) {
// find a player by PlayerName (can be done with any property on the player object)
var playerData = data.find(player => playerName === player.PlayerName);
// if player is found, return the average, else return "N/A"
return playerData ? playerData.Average : "N/A";
}
console.log("Player 1 average: ", getPlayerAverage("Player 1", mapped));
console.log("Player 99 average: ", getPlayerAverage("Player 99", mapped));
console.log("--- mapped data ---");
console.log(mapped);
#1
1
You can map your playerdata
array and calculate the average/best/worst for each player:
您可以映射您的playerdata数组并计算每个播放器的平均/最佳/最差值:
var playerdata = [[2, 'Assault', 1,1,10],
[0, 'Support', 3,2,11],
[1, 'Assault', 7,12,1],
[0, 'Assault', 5,9,14],
[0, 'Assault', 11,17,18]];
var calculated = playerdata.map((player) => {
const rounds = player.slice(2);
return {
player,
average: average(rounds),
best: Math.max(...rounds),
worst: Math.min(...rounds)
};
});
function average(numbers) {
return numbers.reduce((a, b) => a + b, 0) / numbers.length;
}
console.log(calculated);
console.log('Player 1 average', calculated[0].average);
#2
2
While you asked about more round results, you could treat the array from index 2 as data for the rounds.
当您询问更全面的结果时,您可以将索引2中的数组作为轮的数据。
var playerdata = [[2, 'Assault', 1, 1, 10], [0, 'Support', 3, 2, 11], [1, 'Assault', 7, 12, 1], [0, 'Assault', 5, 9, 14], [0, 'Assault', 11, 17, 18]],
result = playerdata.map(player => {
const data = player.slice(2);
return {
player,
average: data.reduce((a, b) => a + b) / data.length,
best: Math.max(...data),
worst: Math.min(...data)
};
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#3
1
What makes the most sense (to me) is to map the data into objects for each player, like so:
(对我来说)最有意义的是将数据映射到每个玩家的对象中,比如:
This solution makes use of:
本解决方案利用:
Array.prototype.map()
(For mapping into array of objects)
array .prototype.map()(用于映射到对象数组)
Array.prototype.reduce()
(For summing the rounds data generically)
reduce()(用于一般地求和round数据)
ES6 Spread Syntax (For gathering/spreading rounds data)
ES6扩展语法(用于收集/扩展回合数据)
ES6 Destructuring Assignment (For easily extracting player data into local variables)
ES6破坏分配(便于将玩家数据提取到局部变量中)
ES6 Find (For plucking a single object out of an array by property value)
ES6 Find(用于按属性值从数组中提取单个对象)
var playerdata = [[2, "Assault", 1,1,10],
[0, "Support", 3,2,11],
[1, "Assault", 7,12,1],
[0, "Assault", 5,9,14],
[0, "Assault", 11,17,18]];
var mapped = playerdata.map(function (arr, index) {
// Extract player data from array through Destructuring
// Same as var NumberOfKeys = arr[0], PreferredCrate = arr[1], etc.
// gather the rest of the arr elements as rounds data
var [NumberOfKeys, PreferedCrate, ...rounds] = arr;
// return an object with the data we want back
return {
PlayerName: "Player " + (index + 1),
NumberOfKeys,
PreferedCrate,
NumberOfRounds: rounds.length,
BestResult: Math.max(...rounds),
WorstResult: Math.min(...rounds),
Average: rounds.reduce((total, round) => total + round) / 3
};
});
function getPlayerAverage(playerName, data) {
// find a player by PlayerName (can be done with any property on the player object)
var playerData = data.find(player => playerName === player.PlayerName);
// if player is found, return the average, else return "N/A"
return playerData ? playerData.Average : "N/A";
}
console.log("Player 1 average: ", getPlayerAverage("Player 1", mapped));
console.log("Player 99 average: ", getPlayerAverage("Player 99", mapped));
console.log("--- mapped data ---");
console.log(mapped);