简体   繁体   English

具有特定键值的对象组数组首先推送

[英]Group array of objects with a specific key value pushed first

Given the following Array of Objects:给定以下对象数组:

[
    {
        "teamFK": 8650,
        "code": "yellow_cards",
        "typeId": 554,
        "value": "5",
        "side": "home"
    },
    {
        "teamFK": 8650,
        "code": "goals",
        "typeId": 554,
        "value": "1",
        "side": "home"
    },
    {
        "teamFK": 8990,
        "code": "yellow_cards",
        "typeId": 555,
        "value": "2",
        "side": "away"
    },
    {
        "teamFK": 8990,
        "code": "goals",
        "typeId": 555,
        "value": "0",
        "side": "away"
    }
]

I would like to group this data by code and get this result:我想按代码对这些数据进行分组并得到这个结果:

{
      "stats": [
        {
          "name": "yellow_cards",
          "stats": ["5","2"]
        },
        {
          "name": "goals",
          "stats": ["2","0"]
        }
      ]
}

What I've done is the following which works but I want to make sure that the alway the stat with "side":"home" always pushed first into the array "stats": [] :我所做的是以下工作,但我想确保总是带有"side":"home"的 stat 总是首先推入数组"stats": []

 const groupedStats = Object.entries(
    query.reduce((acc, { typeId, value, code, side }) => {
      if (!acc[code]) {
        acc[code] = [];
      }

      acc[code].push(value);

      return acc;
    }, {}),
  ).map(([name, stats]) => ({ name, stats }));

My approach is sort it first by side using Array.sort() and then looping through the objects and adding it to stats我的方法是首先使用 Array.sort() 对它进行排序,然后循环遍历对象并将其添加到 stats

i created a const match to find if there is a match already so i dont have to add the name and value again basically if its not a match i'll add it to the stats array and if its a match then i'll just update the current index我创建了一个 const 匹配来查找是否已经存在匹配,所以我基本上不必再次添加名称和值,如果它不匹配,我会将它添加到 stats 数组中,如果它匹配,那么我会更新当前索引

const objs = [
  {
    teamFK: 8650,
    code: "yellow_cards",
    typeId: 554,
    value: "5",
    side: "home",
  },
  {
    teamFK: 8650,
    code: "goals",
    typeId: 554,
    value: "1",
    side: "away",
  },
  {
    teamFK: 8990,
    code: "yellow_cards",
    typeId: 555,
    value: "2",
    side: "away",
  },
  {
    teamFK: 8990,
    code: "goals",
    typeId: 555,
    value: "0",
    side: "home",
  },
];
let stats = [];

const transformedObj = objs
  .sort((a, b) => {
    if (a.side > b.side) {
      return -1;
    }
    if (a.side < b.side) {
      return 1;
    }
    return 0;
  })
  .forEach((obj) => {
    const match = stats.find((stat) => stat.name === obj.code);
    const statsIndex = stats.findIndex((stat) => stat.name === obj.code);
    if (!match) {
      stats = [...stats, { name: obj.code, value: [obj.value] }];
    } else {
      stats[statsIndex] = {
        name: stats[statsIndex].name,
        value: [...stats[statsIndex].value, obj.value],
      };
    }
  });

console.log(stats);

You can sort array and use key grouping approach:您可以对数组进行排序并使用键分组方法:

 const data = [{"teamFK": 8650,"code": "yellow_cards","typeId": 554,"value": "5","side": "home"},{"teamFK": 8650,"code": "goals","typeId": 554,"value": "1","side": "home"},{"teamFK": 8990,"code": "yellow_cards","typeId": 555,"value": "2","side": "away"},{"teamFK": 8990,"code": "goals","typeId": 555,"value": "0","side": "away"}]; const groups = data.sort(({ side: a }, { side: b }) => b.localeCompare(a)).reduce((acc, { code, value }) => { acc[code]??= { name: code, stats: [] }; acc[code]['stats'].push(value); return acc; }, {}); const result = { stats: Object.values(groups) }; console.log(result);
 .as-console-wrapper { max-height: 100%;important: top: 0 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM