将 3 个数字的数组转换为关联的字母

Convert an Array of 3 numbers to associated letters

提问人:DanG 提问时间:1/2/2022 最后编辑:DanG 更新时间:1/2/2022 访问量:78

问:

我正在学习 JS,并为自己设定了一个挑战,即从一系列比赛结果中重新创建足球联赛表。

一切都进展得非常顺利,我几乎完成了,但我无法将我的“FORM GUIDE”数组过滤到它们对应的字母。

我只将过去 5 场比赛的得分输出为数组(如下);

[3, 1, 3, 0, 3]

但我想将其输出为 3 = W、1 = D、0 = L。

所以。。。 西、中、西、左、西

有人可以解释一下我该怎么做吗?

谢谢

'use strict';

// Tottenham's Premier League scores & points 21-22 //
//--------------------------------------------------//

const scoresPoints = [
    [1, 0, 3], // Watford
    [1, 1, 1], // Southampton
    [3, 0, 3], // Crystal Palace
    [2, 2, 1], // Liverpool
    [3, 0, 3], // Norwich
    [2, 0, 3], // Brentford
    [2, 1, 3], // Leeds
    [0, 0, 1], // Everton
    [3, 2, 3], // Newcastle
    [0, 3, 0], // Man U
    [0, 1, 0], // West Ham
    [2, 1, 3], // Villa
    [1, 3, 0], // Arsenal
    [0, 3, 0], // Chelsea
    [0, 3, 0], // Crystal Palace
    [1, 0, 3], // Watford
    [1, 0, 3], // Wolves
    [1, 0, 3], // Man City
];

// Define The functions & arrays--------------------//
//--------------------------------------------------//

let tottenhamScores;
let totalTottenhamGoals;
let totalTottenhamPoints;
let totalOpponentsGoals;
let tottenhamForm = [];
// The goals scored by Tottenham
const tottenhamGoals = [];
// The points scored by Tottenham
const tottenhamPoints = [];
// The goals scored by the opponents
const opponentsGoals = [];

// Filter the data from each array------------------//
//--------------------------------------------------//

for (let i = 0; i < scoresPoints.length; i++) {
    tottenhamScores = scoresPoints[i][0];
    // Just Tottenham's goals
    tottenhamGoals.push(tottenhamScores);
    // Just Tottenham's points
    const leaguePoints = scoresPoints[i][2];
    tottenhamPoints.push(leaguePoints);
    // Just the opponents goals
    const opponentsScores = scoresPoints[i][1];
    opponentsGoals.push(opponentsScores);
    // Just Tottenham's Form
    const leagueForm = scoresPoints[i][2];
    tottenhamForm.push(leagueForm);
}

// Adding up the arrays-----------------------------//
//--------------------------------------------------//

// Adding up Tottenham's goals
for (let i = 0; i < tottenhamGoals.length; i++) {
    totalTottenhamGoals = tottenhamGoals.reduce(function (a, b) {
        return a + b;
    }, 0);
}
// Adding up Tottenham's points
for (let i = 0; i < tottenhamPoints.length; i++) {
    totalTottenhamPoints = tottenhamPoints.reduce(function (a, b) {
        return a + b;
    }, 0);
}
// Adding up the opponents goals
for (let i = 0; i < opponentsGoals.length; i++) {
    totalOpponentsGoals = opponentsGoals.reduce(function (a, b) {
        return a + b;
    }, 0);
}

// Last 5 games-------------------------------------//
//--------------------------------------------------//

// Find the individual values
function occurrence(pointValues, value) {
    return pointValues.filter(v => v === value).length;
}
const win = occurrence(tottenhamPoints, 3);
const draw = occurrence(tottenhamPoints, 1);
const loss = occurrence(tottenhamPoints, 0);

// Filter to last five games
function lastFiveGames(form, five) {
    form = tottenhamForm.slice(0, five);
    return form;
}
const latestForm = lastFiveGames(tottenhamForm, 5);

// Convert points to represented letters
const letteredResult = latestForm.map(result => {
    switch (result) {
        case 0:
            return 'L';
        case 1:
            return 'D';
        case 3:
            return 'W';
        default:
            return 'No Form To Display';
    }
});

// Print the statement & table----------------------//
//--------------------------------------------------//

console.log(
    `SUMMARY
-------- 
-------- 
Throughout the 2021-22 Premier League season, Tottenham Hotspur have scorred ${totalTottenhamGoals} goals and conceeded ${totalOpponentsGoals}.
    
This has gained Tottenham Hotspur ${totalTottenhamPoints} points to date.

(Dropping ${
        scoresPoints.length * 3 - totalTottenhamPoints
    } points throughout the season from the maximum of ${
        scoresPoints.length * 3
    } available).
   
    TABLE & FORM GUIDE ---
    ----------------------
    ${scoresPoints.length} Played
    ${win} Wins
    ${draw} Draws
    ${loss} Losses
    ${totalTottenhamGoals} For
    ${totalOpponentsGoals} Against
    ${totalTottenhamGoals - totalOpponentsGoals} Goal Difference
    ${totalTottenhamPoints} POINTS IN TOTAL
    
    FORM (Last 5 Games) ${letteredResult}
    ----------------------
    ----------------------`
);
JavaScript 数组

评论

0赞 Shub 1/2/2022
你尝试过什么吗?
0赞 Ganesh Babu 1/2/2022
你能详细说明一下这里的逻辑吗?如果点是 4 怎么办?
0赞 Andreas 1/2/2022
Stack Overflow 用户需要付出多少研究努力?
0赞 DanG 1/2/2022
4 永远不会出现,因为唯一可用的点是 3,1,0。我用我有限的知识尝试了很多解决方案,但没有运气,但最后一个使用地图的答案很有效。我将研究这两种解决方案并弄清楚它们是如何工作的。谢谢你的帮助。
0赞 DanG 1/2/2022
我已经将我编写的所有代码添加到初始问题中,并使用 ABDULLOKH 的简单 switch 语句来解决这个问题。我似乎被指责没有付出自己的努力,(当我真的)@Shubaker。对不起,我应该在最初的问题中发布我尝试编写的代码,如果我冒犯了你,对不起。

答:

1赞 Ebay 1/2/2022 #1

let arr = [3, 1, 3, 0, 3];

let arrMap = arr.map((i) => {
  if (i == 3) return "W";
  if (i == 0) return "L";
  return "D"
})


console.log(arrMap)

1赞 konsolebox 1/2/2022 #2

您可以使用地图:

[3, 1, 3, 0, 3].map(e => { if (e == 3) return "W";  if (e == 1) return "D"; if (e == 0) return "L"; throw `Unexpected value: ${e}`; })

评论

0赞 Andreas 1/2/2022
就像这个答案一样。为什么这个不费吹灰之力的问题首先值得回答?
1赞 konsolebox 1/2/2022
@Andreas 另一个答案过于简单化了。
0赞 Andreas 1/2/2022
另一个答案与您的答案完全相同 - 减去不必要的:“4 永远不会发生,因为唯一可用的点是 3,1,0”throw
0赞 konsolebox 1/2/2022
@Andreas 这是你的编码意见。无论可能性如何,都应始终一致地处理变量输入。
0赞 DanG 1/2/2022
谢谢。这很有效,我会把它拆开以更好地理解箭头功能,但谢谢你的帮助。
2赞 ABDULLOKH MUKHAMMADJONOV 1/2/2022 #3

你可以通过一个很好的声明来实现这一目标。试试这个方法:Array.mapswitch

const arr = [3, 1, 3, 0, 3]

const matched = arr.map(result => {
  switch(result){
    case 0:
      return 'L'
    case 1:
      return 'D'
    case 3:
      return 'W'
    default:
      return 'X'
  }
})

console.log(matched)

0赞 Ran Turner 1/2/2022 #4

您可以使用充当枚举的对象来实现这种干净简单的方法,其中键将表示点,值将表示结果字符。map

const array = [3, 1, 3, 0, 3];
const cases = {0: 'L', 1: 'D', 3: 'W'};

const matches = array.map(e => cases[e]);

console.log(matches)

更多信息 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/mapmap

0赞 Dirk J. Faber 1/2/2022 #5

除了使用 ,您还可以使用 .主要区别在于将返回一个新数组,而不返回任何内容,可用于更改原始数组。方法如下:mapforEachmapforEach

const logic = {
  3: 'W',
  1: 'D',
  0: 'L'
}

const data = [3, 1, 3, 0, 3];

data.forEach((value, key) => data[key] = logic[value]);

console.log(data);

0赞 PeterKA 1/2/2022 #6

const arr = [3, 1, 3, 0, 3, 2, 4];
const data = arr.map(v => ['L','D','_','W'][v] || '_');

console.log( data );