根据通用属性值合并数组

时间:2019-01-17 15:44:16

标签: javascript arrays object vue.js vuejs2

我正在编写一个由VueJS和ExpressionEngine驱动的小应用程序,它显示高尔夫锦标赛排行榜。锦标赛可能要求您玩多个课程。来自数据库的数据如下所示(几轮)。

问题是我需要重组rounds数组,使其看起来像排行榜数组。我对如何完成此工作有一些阻碍。任何指导将不胜感激!

rounds: [
   {
      team: "Dropkick Murphies",
      score: 75,
      course: 17
   },
   {
      team: "Dropkick Murphies",
      score: 62,
      course: 24
   },
   {
      team: "Dropkick Murphies",
      score: 69,
      course: 26
   },
   {
      team: "Gigantic",
      score: 67,
      course: 26
   },
   {
      team: "Gigantic",
      score: 65,
      course: 17
   },
   {
      team: "Gigantic",
      score: 63,
      course: 24
   }
]
leaderboard: [
   {
      team: 'Dropkick Murphys',
      rounds: [
         {
            score: 75,
            course: 17
         },
         {
            score: 62,
            course: 24
         },
         {
            score: 69,
            course: 26
         },
   },
   {
      team: 'Wooden Jellyfishes',
      rounds: [
         {
            score: 68,
            course: 24
         },
         {
            score: 65,
            course: 17
         },
         {
            score: 70,
            course: 26
         }
   },
]

5 个答案:

答案 0 :(得分:0)

一种解决方法是使用reduce()。在每次迭代中,您可以检查当前分析的对象是否已存在于累积数组中(即,具有相同team name的对象已存在)。然后,您可以使用此条件将新的整个对象推入累积的数组中,或仅向rounds数组中添加新的数据集:

const rounds = [
   {team: "Dropkick Murphies", score: 75, course: 17},
   {team: "Dropkick Murphies", score: 62, course: 24},
   {team: "Dropkick Murphies", score: 69, course: 26},
   {team: "Gigantic", score: 67, course: 26},
   {team: "Gigantic", score: 65, course: 17},
   {team: "Gigantic", score: 63, course: 24}
];

let res = rounds.reduce((r, {team, score, course}) =>
{
   let found = r.findIndex(x => x.team === team);
   
   if (found >= 0)
       r[found].rounds.push({score: score, course: course});
   else
       r.push({team: team, rounds: [{score: score, course: course}]});

   return r;

}, []);

console.log(res);

答案 1 :(得分:0)

您可以实现转换的辅助功能。

function convertRoundsToLeaderBoard(rounds) {
    var indexes = {};
    var leaderBoard = [];
    for (var round of rounds) {
        if (indexes[round.team] === undefined) {
            indexes[round.team] = leaderBoard.length;
            leaderBoard.push({
                team: round.team,
                rounds: [{score: round.score, course: round.course}]
            });
        } else {
            leaderBoard[indexes[round.team]].rounds.push({score: round.score, course: round.course});
        }
    }
    return leaderBoard;
}

让我们测试一下:

convertRoundsToLeaderBoard(
[
   {
      team: "Dropkick Murphies",
      score: 75,
      course: 17
   },
   {
      team: "Dropkick Murphies",
      score: 62,
      course: 24
   },
   {
      team: "Dropkick Murphies",
      score: 69,
      course: 26
   },
   {
      team: "Gigantic",
      score: 67,
      course: 26
   },
   {
      team: "Gigantic",
      score: 65,
      course: 17
   },
   {
      team: "Gigantic",
      score: 63,
      course: 24
   }
]);

答案 2 :(得分:0)

我建议使用Map按团队为您键入数据。这样,您就可以使用具有线性时间复杂度的forEach循环来构建数据:

const rounds = [{team: "Dropkick Murphies",score: 75,course: 17},{team: "Dropkick Murphies",score: 62,course: 24},{team: "Dropkick Murphies",score: 69,course: 26},{team: "Gigantic",score: 67,course: 26},{team: "Gigantic",score: 65,course: 17},{team: "Gigantic",score: 63,course: 24}];

// Create Map with one entry per team
const map = new Map(rounds.map(({team}) => [team, []]));
// Populate each team with their scores
rounds.forEach(({team,score,course}) => map.get(team).push({score,course}));
// Convert the Map to the desired data structure
const result = Array.from(map, ([team, rounds]) => ({team, rounds}));

console.log(result);

答案 3 :(得分:0)

可能不是最优雅的解决方案,但这是另一个:

var data = {
    rounds: [{
            team: "Dropkick Murphies",
            score: 75,
            course: 17
        },
        ...
    ]
};

var groups = {};
for (var o of data.rounds) {
    if (!groups[o.team])
        groups[o.team] = [];
    groups[o.team].push({
        score: o.score,
        course: o.course
    });
}
var result = [];
for (var g in groups) {
    result.push({
        team: g,
        rounds: groups[g]
    });
}

答案 4 :(得分:0)

您可以使用reduce来实现。

这里我们使用 team name 作为键属性,并检查它是否已经存在于输出对象中,我们按下 score course ,如果没有,则在该特定团队密钥的rounds属性中创建新密钥。

const rounds = [
   {team: "Dropkick Murphies", score: 75, course: 17},
   {team: "Dropkick Murphies", score: 62, course: 24},
   {team: "Dropkick Murphies", score: 69, course: 26},
   {team: "Gigantic", score: 67, course: 26},
   {team: "Gigantic", score: 65, course: 17},
   {team: "Gigantic", score: 63, course: 24}
];

let op = rounds.reduce((output,current)=>{
  if(output[current.team]){
    output[current.team].rounds.push({
      score : current.score,
      course: current.score
    })} else {
      output[current.team] = {
        team: current.team,
        rounds: [{score:current.score, course:current.course}]
      }
    }
 return output 
},{})

console.log(Object.values(op))