解析和修改 Javascript 中的数组

Parse and modify an array in Javascript

我在 Javascript 中处理一个 .dat 文件,其中包含有关英国足球队的不同数据。我能够获得我在此数组中转换的 Json:

[{"team":"Arsenal","f":"79","a":"36"},
{"team":"Liverpool","f":"67","a":"30"},
{"team":"Manchester_U","f":"87","a":"45"},
{"team":"Newcastle","f":"74","a":"52"},
{"team":"Leeds","f":"53","a":"37"},
{"team":"Chelsea","f":"66","a":"38"},
{"team":"West_Ham","f":"48","a":"57"},
{"team":"Aston_Villa","f":"46","a":"47"},
{"team":"Tottenham","f":"49","a":"53"},
{"team":"Blackburn","f":"55","a":"51"},
{"team":"Southampton","f":"46","a":"54"},
{"team":"Middlesbrough","f":"35","a":"47"},
{"team":"Fulham","f":"36","a":"44"},
{"team":"Charlton","f":"38","a":"49"},
{"team":"Everton","f":"45","a":"57"},
{"team":"Bolton","f":"44","a":"62"},
{"team":"Sunderland","f":"29","a":"51"},
{"team":"Ipswich","f":"41","a":"64"},
{"team":"Derby","f":"33","a":"63"},
{"team":"Leicester","f":"30","a":"64"}]

此数组包含不同足球队的名称和两个值:每支球队的进球数 (F) 和失球数 (A)。我必须解析此数组以进行以下操作:

  1. 将“a”和“f”之间的差值添加到数组(对于非常团队);
  2. Return进球数与失球数之差最小的球队名称。

但是我不知道怎么办。

我只到这里:

const myFrom = document.getElementById("myForm");
const datFile = document.getElementById("datFile");

function fileToArray(text) {
  const lines = text.split(/\n/); // split on \n
  const header = lines[0].trim().split(/\s+/); // split on whitespace after trimming
  return [...lines.slice(1).map(line => {
    // ignore the number and the dash
    const [number, team, p, w, l, d, f, ignore2, a, pts] = line.trim().split(/\s+/);
    let values={team, f, a};
    return values;
  })]
  
}

myForm.addEventListener("submit", function (e) {
    e.preventDefault();
    console.log("Form submitted");
   const input = datFile.files[0];
   const reader = new FileReader();
   reader.onload = function (e){
     const text= e.target.result;
     const data = fileToArray(text);
     const arr1 = data.slice(0,17);
     const arr2 = data.slice(18,21);
    let myArray = arr1.concat(arr2);
    let myJSON = JSON.stringify(myArray);
    let obj=JSON.parse(myJSON);
   }
   
   reader.readAsText(input);
  });
 <form id="myForm">
        <input type="file" id="datFile" accept=".dat" />
        <br />
        <input type="submit" value="Submit" />
      </form>

到这里我就停了

认为这两个操作可以一起完成以进行干净的书写,您可以执行以下操作。

map 将两个 属性 之间的差异添加到名为 d

的新 属性 中

使用 Math.abs 保持正差。

let teams = [{"team":"Arsenal","f":"79","a":"36"},
{"team":"Liverpool","f":"67","a":"30"},
{"team":"Manchester_U","f":"87","a":"45"},
{"team":"Newcastle","f":"74","a":"52"},
{"team":"Leeds","f":"53","a":"37"},
{"team":"Chelsea","f":"66","a":"38"},
{"team":"West_Ham","f":"48","a":"57"},
{"team":"Aston_Villa","f":"46","a":"47"},
{"team":"Tottenham","f":"49","a":"53"},
{"team":"Blackburn","f":"55","a":"51"},
{"team":"Southampton","f":"46","a":"54"},
{"team":"Middlesbrough","f":"35","a":"47"},
{"team":"Fulham","f":"36","a":"44"},
{"team":"Charlton","f":"38","a":"49"},
{"team":"Everton","f":"45","a":"57"},
{"team":"Bolton","f":"44","a":"62"},
{"team":"Sunderland","f":"29","a":"51"},
{"team":"Ipswich","f":"41","a":"64"},
{"team":"Derby","f":"33","a":"63"},
{"team":"Leicester","f":"30","a":"64"}];

teams.map((t) => t['d'] = Math.abs(t.f - t.a))

console.log(teams);

要减少到具有最低差异的团队,您可以使用以下代码 reduce

    let teams = [{"team":"Arsenal","f":"79","a":"36"},
    {"team":"Liverpool","f":"67","a":"30"},
    {"team":"Manchester_U","f":"87","a":"45"},
    {"team":"Newcastle","f":"74","a":"52"},
    {"team":"Leeds","f":"53","a":"37"},
    {"team":"Chelsea","f":"66","a":"38"},
    {"team":"West_Ham","f":"48","a":"57"},
    {"team":"Aston_Villa","f":"46","a":"47"},
    {"team":"Tottenham","f":"49","a":"53"},
    {"team":"Blackburn","f":"55","a":"51"},
    {"team":"Southampton","f":"46","a":"54"},
    {"team":"Middlesbrough","f":"35","a":"47"},
    {"team":"Fulham","f":"36","a":"44"},
    {"team":"Charlton","f":"38","a":"49"},
    {"team":"Everton","f":"45","a":"57"},
    {"team":"Bolton","f":"44","a":"62"},
    {"team":"Sunderland","f":"29","a":"51"},
    {"team":"Ipswich","f":"41","a":"64"},
    {"team":"Derby","f":"33","a":"63"},
    {"team":"Leicester","f":"30","a":"64"}];
    teams.map((t) => t['d'] = Math.abs(t.f - t.a))
    
    // now finding the team with the lowsest
    
    let result = teams.reduce((teamA,teamB) => teamA.d > teamB.d? teamB: teamA );

    console.log(result);

map 和 reduce 的组合可以做到:

const teams= [{"team":"Arsenal","f":"79","a":"36"}, {"team":"Liverpool","f":"67","a":"30"}, {"team":"Manchester_U","f":"87","a":"45"}, {"team":"Newcastle","f":"74","a":"52"}, {"team":"Leeds","f":"53","a":"37"}, {"team":"Chelsea","f":"66","a":"38"}, {"team":"West_Ham","f":"48","a":"57"}, {"team":"Aston_Villa","f":"46","a":"47"}, {"team":"Tottenham","f":"49","a":"53"}, {"team":"Blackburn","f":"55","a":"51"}, {"team":"Southampton","f":"46","a":"54"}, {"team":"Middlesbrough","f":"35","a":"47"}, {"team":"Fulham","f":"36","a":"44"}, {"team":"Charlton","f":"38","a":"49"}, {"team":"Everton","f":"45","a":"57"}, {"team":"Bolton","f":"44","a":"62"}, {"team":"Sunderland","f":"29","a":"51"}, {"team":"Ipswich","f":"41","a":"64"}, {"team":"Derby","f":"33","a":"63"}, {"team":"Leicester","f":"30","a":"64"}]

let diff=Infinity;
console.log(
  teams.map((t)=>(t.d=t.a-t.f, t))
  .reduce((a,c)=>Math.abs(c.d)<=diff?(diff=Math.abs(c.d), c.team): a,'')
);

使用循环(例如 for...of )或数组方法(例如第 1 部分的映射和第 2 部分的过滤器)都是有效的方法。

我个人更喜欢循环,因为它更易于阅读和调试,而且您可以将两个步骤合并为一个循环。类似于:

// use the first element as starter then update in the loop
let leastDiff = Math.abs(myArray[0].f - myArray[0].a);
let bestTeam = myArray[0].team;
for (let item of myArray) {
  item.diff = Math.abs(item.f - item.a);
  if (item.diff < leastDiff) {
    leastDiff = item.diff;
    bestTeam = item.team;
  }
}
console.log(`Team with least difference is ${bestTeam} with ${leastDiff} goal difference`);