js问题没有思路,我先用filter选出女生和90分以上的数据,但是我不知道如何把数组分组,下面步骤如何做?

2023-03-28 0 2,507
{
  name:
  sex:
  age:
  subject:
  score:
}

当前有一个数组array里以上面的结构存放了一些数据, 用js实现以下需求
返回每个科目分数都在90以上的所有女同学的数组,并根据语文分数和同学姓名排序

const array = [
  {
    name: "吕浩龙",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 90,
  },
  {
    name: "吕浩龙",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 76,
  },
  {
    name: "吕浩龙",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 86,
  },
  {
    name: "张明",
    sex: "女",
    age: 17,
    subject: "语文",
    score: 78,
  },
  {
    name: "张明",
    sex: "女",
    age: 17,
    subject: "数学",
    score: 98,
  },
  {
    name: "张明",
    sex: "女",
    age: 17,
    subject: "英语",
    score: 70,
  },
  {
    name: "邱海",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 90,
  },
  {
    name: "邱海",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 89,
  },   
   {
    name: "邱海",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 67,
  },
  {
    name: "龙先旺",
    sex: "女",
    age: 17,
    subject: "英语",
    score: 96,
  },
  {
    name: "龙先旺",
    sex: "女",
    age: 17,
    subject: "数学",
    score: 94,
  },  {
    name: "龙先旺",
    sex: "女",
    age: 17,
    subject: "语文",
    score: 89,
  },
  {
    name: "李子舰",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 98,
  },
  {
    name: "李子舰",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 96,
  },      {
    name: "李子舰",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 90,
  },
  {
    name: "杜亚晨",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 90,
  },
  {
    name: "杜亚晨",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 96,
  },    {
    name: "杜亚晨",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 94,
  },
  {
    name: "焦季雅",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 96,
  },     {
    name: "焦季雅",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 78,
  },     {
    name: "焦季雅",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 65,
  },
  {
    name: "侯悠馨",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 56,
  },
  {
    name: "侯悠馨",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 34,
  },   {
    name: "侯悠馨",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 70,
  },
  {
    name: "车语薇",
    sex: "男",
    age: 16,
    subject: "语文",
    score: 67,
  },
  {
    name: "车语薇",
    sex: "男",
    age: 16,
    subject: "数学",
    score: 54,
  },  {
    name: "车语薇",
    sex: "男",
    age: 16,
    subject: "英语",
    score: 96,
  },
  {
    name: "牧美玲",
    sex: "男",
    age: 17,
    subject: "英语",
    score: 90,
  },
  {
    name: "牧美玲",
    sex: "男",
    age: 17,
    subject: "语文",
    score: 98,
  },   {
    name: "牧美玲",
    sex: "男",
    age: 17,
    subject: "数学",
    score: 99,
  },
  {
    name: "吕平蝶",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 93,
  },
  {
    name: "吕平蝶",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 95,
  },   {
    name: "吕平蝶",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 96,
  },
  {
    name: "曾妮娜",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 45,
  },
  {
    name: "曾妮娜",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 89,
  },    {
    name: "曾妮娜",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 65,
  },
  {
    name: "韩怡月",
    sex: "男",
    age: 16,
    subject: "英语",
    score: 97,
  },
  {
    name: "韩怡月",
    sex: "男",
    age: 16,
    subject: "语文",
    score: 95,
  },  
   {
    name: "韩怡月",
    sex: "男",
    age: 16,
    subject: "数学",
    score: 96,
  },
  {
    name: "印溪澈",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 94,
  },
  {
    name: "印溪澈",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 91,
  },   {
    name: "印溪澈",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 95,
  },
  {
    name: "邹莹莹",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 72,
  },
  {
    name: "邹莹莹",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 79,
  },   {
    name: "邹莹莹",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 73,
  },
  {
    name: "简春晓",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 35,
  },
  {
    name: "简春晓",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 87,
  },   {
    name: "简春晓",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 65,
  },
  {
    name: "慕天恩",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 98,
  },
  {
    name: "慕天恩",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 99,
  },   
   {
    name: "慕天恩",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 100,
  },
  {
    name: "冉平蝶",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 74,
  },
  {
    name: "冉平蝶",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 65,
  },   {
    name: "冉平蝶",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 78,
  },
  {
    name: "向海冬",
    sex: "男",
    age: 16,
    subject: "英语",
    score: 67,
  },
  {
    name: "向海冬",
    sex: "男",
    age: 16,
    subject: "数学",
    score: 86,
  },   {
    name: "向海冬",
    sex: "男",
    age: 16,
    subject: "语文",
    score: 69,
  },
  {
    name: "龙琰琬",
    sex: "男",
    age: 17,
    subject: "英语",
    score: 93,
  },
  {
    name: "龙琰琬",
    sex: "男",
    age: 17,
    subject: "数学",
    score: 97,
  },   {
    name: "龙琰琬",
    sex: "男",
    age: 17,
    subject: "语文",
    score: 93,
  },
  {
    name: "朱典雅",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 90,
  },
  {
    name: "朱典雅",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 94,
  },   {
    name: "朱典雅",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 99,
  },
  {
    name: "方双儿",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 69,
  },
  {
    name: "方双儿",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 63,
  },      {
    name: "方双儿",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 64,
  },
  {
    name: "杨暖姝",
    sex: "男",
    age: 15,
    subject: "英语",
    score: 77,
  },
  {
    name: "杨暖姝",
    sex: "男",
    age: 15,
    subject: "语文",
    score: 73,
  },    {
    name: "杨暖姝",
    sex: "男",
    age: 15,
    subject: "数学",
    score: 67,
  },
  {
    name: "汤曼文",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 93,
  },
  {
    name: "汤曼文",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 92,
  },
  {
    name: "汤曼文",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 97,
  },
  {
    name: "龙灵萱",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 79,
  },
  {
    name: "龙灵萱",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 38,
  },   {
    name: "龙灵萱",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 98,
  },
  {
    name: "益曼荷",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 67,
  },
  {
    name: "益曼荷",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 89,
  },  {
    name: "益曼荷",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 77,
  },
  {
    name: "汤映安",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 89,
  },
  {
    name: "汤映安",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 90,
  },
  {
    name: "汤映安",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 91,
  },
  {
    name: "宰千易",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 95,
  },
  {
    name: "宰千易",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 98,
  },  {
    name: "宰千易",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 99,
  },
  {
    name: "古莹莹",
    sex: "男",
    age: 16,
    subject: "英语",
    score: 96,
  },
  {
    name: "古莹莹",
    sex: "男",
    age: 16,
    subject: "语文",
    score: 93,
  },
  {
    name: "古莹莹",
    sex: "男",
    age: 16,
    subject: "数学",
    score: 91,
  },
  {
    name: "苏和怡",
    sex: "男",
    age: 17,
    subject: "英语",
    score: 93,
  },
  {
    name: "苏和怡",
    sex: "男",
    age: 17,
    subject: "语文",
    score: 92,
  },
  {
    name: "苏和怡",
    sex: "男",
    age: 17,
    subject: "数学",
    score: 96,
  },
  {
    name: "国书双",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 94,
  },
  {
    name: "国书双",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 99,
  },
  {
    name: "国书双",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 100,
  },
  {
    name: "巴芷珊",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 62,
  },
  {
    name: "巴芷珊",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 48,
  },
  {
    name: "巴芷珊",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 87,
  },
  {
    name: "乌石瑶",
    sex: "男",
    age: 15,
    subject: "英语",
    score: 76,
  },
  {
    name: "乌石瑶",
    sex: "男",
    age: 15,
    subject: "语文",
    score: 73,
  },
  {
    name: "乌石瑶",
    sex: "男",
    age: 15,
    subject: "数学",
    score: 79,
  },
  {
    name: "卢元风",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 96,
  },
  {
    name: "卢元风",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 95,
  },      {
    name: "卢元风",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 94,
  },
  {
    name: "杜裕梅",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 98,
  },
  {
    name: "杜裕梅",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 90,
  },  {
    name: "杜裕梅",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 94,
  },
  {
    name: "乔步美",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 93,
  },
  {
    name: "乔步美",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 91,
  },
  {
    name: "乔步美",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 100,
  },
  {
    name: "容亦丝",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 68,
  },
  {
    name: "容亦丝",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 87,
  },
  {
    name: "容亦丝",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 34,
  },
  {
    name: "吴夏璇",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 89,
  },
  {
    name: "吴夏璇",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 74,
  },
  {
    name: "吴夏璇",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 63,
  },
  {
    name: "禄凡双",
    sex: "男",
    age: 16,
    subject: "英语",
    score: 87,
  },
  {
    name: "禄凡双",
    sex: "男",
    age: 16,
    subject: "语文",
    score: 89,
  },
  {
    name: "禄凡双",
    sex: "男",
    age: 16,
    subject: "数学",
    score: 74,
  },
  {
    name: "甘一璇",
    sex: "男",
    age: 17,
    subject: "英语",
    score: 90,
  },
  {
    name: "甘一璇",
    sex: "男",
    age: 17,
    subject: "数学",
    score: 87,
  },  {
    name: "甘一璇",
    sex: "男",
    age: 17,
    subject: "语文",
    score: 96,
  },
  {
    name: "符杨柳",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 90,
  },
  {
    name: "符杨柳",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 98,
  },     {
    name: "符杨柳",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 94,
  },
  {
    name: "储书意",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 89,
  },
  {
    name: "储书意",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 98,
  },
  {
    name: "储书意",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 95,
  },
  {
    name: "瞿涵涵",
    sex: "男",
    age: 15,
    subject: "英语",
    score: 67,
  },
  {
    name: "瞿涵涵",
    sex: "男",
    age: 15,
    subject: "语文",
    score: 98,
  },
  {
    name: "瞿涵涵",
    sex: "男",
    age: 15,
    subject: "数学",
    score: 64,
  },
  {
    name: "蒋嫣然",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 96,
  },
  {
    name: "蒋嫣然",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 93,
  },
  {
    name: "蒋嫣然",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 91,
  },
  {
    name: "乔晏如",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 78,
  },
  {
    name: "乔晏如",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 78,
  },
  {
    name: "乔晏如",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 78,
  },
  {
    name: "阴孟夏",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 90,
  },
  {
    name: "阴孟夏",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 90,
  },
  {
    name: "阴孟夏",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 90,
  },
  {
    name: "寿韶仪",
    sex: "女",
    age: 18,
    subject: "英语",
    score: 68,
  },
  {
    name: "寿韶仪",
    sex: "女",
    age: 18,
    subject: "语文",
    score: 87,
  },
  {
    name: "寿韶仪",
    sex: "女",
    age: 18,
    subject: "数学",
    score: 48,
  },
  {
    name: "终凝然",
    sex: "男",
    age: 18,
    subject: "英语",
    score: 65,
  },
  {
    name: "终凝然",
    sex: "男",
    age: 18,
    subject: "语文",
    score: 74,
  },
  {
    name: "终凝然",
    sex: "男",
    age: 18,
    subject: "数学",
    score: 98,
  },
  {
    name: "池香菱",
    sex: "男",
    age: 16,
    subject: "英语",
    score: 94,
  },
  {
    name: "池香菱",
    sex: "男",
    age: 16,
    subject: "语文",
    score: 92,
  },
  {
    name: "池香菱",
    sex: "男",
    age: 16,
    subject: "数学",
    score: 93,
  },
];
js问题没有思路,我先用filter选出女生和90分以上的数据,但是我不知道如何把数组分组,下面步骤如何做?
 // filter筛选出女的, 使用sort 对筛选出来的排序
  let girl = array.filter(item => item.sex == '女' && item.score >= 90).
    sort((a, b) => {
      if (a.subject == '语文' && b.subject == '语文') {
//         根据语文分数排序
        return b.score - a.score;
      } else {
        return -1
      }
    })

可以借助 Map 结构来合并各科成绩,并且使用 filter() 和 sort() 两个API来简单的实现过滤和排序功能:

// 合并同学的三科成绩
const studentMap = new Map()
array.forEach(student => {
  if (studentMap.has(student.name)) {
    studentMap.set(student.name, {...studentMap.get(student.name), [student.subject]: student.score})
  } else {
    studentMap.set(student.name, {...student, [student.subject]: student.score})
  }
})

// 筛选所有女同学,且三科都大于等于90分
const filterResult =  Array.from(studentMap.values()).filter(student => student.sex==="女" && student.英语 >= 90 && student.语文 >= 90 && student.数学 >= 90)

// 按照语文成绩排序,若语文成绩相同,则按照姓名排序
const sortFilterResult = filterResult.sort((a, b) => {
  if (a.语文 === b.语文) {
    return a.name.localeCompare(b.name);
  } else {
    return b.语文 - a.语文;
  }
})

console.log(filterResult)

以下是输出结果

[
  { name: '慕天恩', sex: '女', age: 18, subject: '英语', score: 98, '英语': 98, '语文': 99, '数学': 100},
  { name: '杜裕梅', sex: '女', age: 18, subject: '英语', score: 98, '英语': 98, '语文': 90, '数学': 94},
  { name: '李子舰', sex: '女', age: 18, subject: '英语', score: 98, '英语': 98, '数学': 96, '语文': 90}
]

P.S. 直接使用 localeCompare 排序会有一些隐患,排序的结果并不一定就会按照找 ABCD 这样排序。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. 有软官网所有资源来源于开发团队,加入会员即可下载使用!如有问题请联系右下角在线客服!
2. 有软官方保障所有软件都通过人工亲测,为每位会员用户提供安全可靠的应用软件、游戏资源下载及程序开发服务。
3. 有软团队针对会员诉求,历经多年拥有现今开发成果, 每款应用程序上线前都经过人工测试无误后提供安装使用,只为会员提供安全原创的应用。
4. PC/移动端应用下载后如遇安装使用问题请联系右下角在线客服或提交工单,一对一指导解决疑难。

有软官网_用软件,找有软 技术分享 js问题没有思路,我先用filter选出女生和90分以上的数据,但是我不知道如何把数组分组,下面步骤如何做? https://www.jiaruvip.com/1052.html

有软应用商店是经过官方安全认证,保障正版软件平台

相关资源

官方客服团队

为您解决烦忧 - 24小时在线 专业服务