js问题没有思路,我先用filter选出女生和90分以上的数据,但是我不知道如何把数组分组,下面步骤如何做?
{
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,
},
];
回复
1个回答
test
2024-07-06
先解题
注:题上说 90 分以上,按条件找出来只有一个女生符合条件,所以我猜应该是 90 分及以上,找出来是 3 条数据
// 第一步,按姓名分组,得到一个对象(Map)
const groups = array.reduce((groups, it) => ((groups[it.name] ??= []).push(it), groups), {});
// 第二步,转换成数组
// 得到 [name, it[]][] 这样的数组(pair 数组)
const result = Object.entries(groups)
// 根据 pair 的值(对象列表)来过滤,
// 1. 要是女生(its 数组随便一个成员对象的 sex 是女
// 2. 要每一科(every)都大于 90
.filter(([, its]) => its[0].sex ==="女" && its.every(({ score }) => score >= 90))
// 再根据语言成绩(在 its 里找语言,取成绩),和姓名排序
.sort((a, b) => {
const [aName, aIts] = a;
const [bName, bIts] = b;
const aScore = aIts.find(({ subject }) => subject === "语文")?.score ?? 0;
const bScore = bIts.find(({ subject }) => subject === "语文")?.score ?? 0;
if (aScore === bScore) {
// 语文分数相同的情况下,按姓名(字母升序)
return aName < bName ? -1 : aName > bName ? -1 : 0;
} else {
// 按语文分数从大到小(降序)
return bScore - aScore;
}
});
结果
[
[
'慕天恩',
[
{ 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: 98 },
{ name: '杜裕梅', sex: '女', age: 18, subject: '语文', score: 90 },
{ name: '杜裕梅', sex: '女', age: 18, subject: '数学', score: 94 }
]
],
[
'李子舰',
[
{ name: '李子舰', sex: '女', age: 18, subject: '英语', score: 98 },
{ name: '李子舰', sex: '女', age: 18, subject: '数学', score: 96 },
{ name: '李子舰', sex: '女', age: 18, subject: '语文', score: 90 }
]
]
]
但是这个结果其实并不好看。根据这个结果,再结合题意,我认为只需要把过滤出来的这些同学的含语文成绩那个数据列出来就好,所以可以在排序前先筛出来
再切题意
// 第二步,转换成数组
// 得到 [name, it[]][] 这样的数组(pair 数组)
const result = Object.entries(groups)
// 根据 pair 的值(对象列表)来过滤,
// 1. 要是女生(its 数组随便一个成员对象的 sex 是女
// 2. 要每一科(every)都大于 90
.filter(([, its]) => its[0].sex === "女" && its.every(({ score }) => score >= 90))
// 在 pair 的 value 中查找语文,如果找不到会返回 undefined
.map(([, its]) => its.find(({ subject }) => subject === "语文"))
// 过滤掉可能存在的 undefined(万一哪个人语文缺考呢……😂)
.filter(it => it)
// 再根据语言成绩(在 its 里找语言,取成绩),和姓名排序
.sort((a, b) => {
// 这时候的 a、b 已经不是 pair,而是对象了
const { name: aName, score: aScore } = a;
const { name: bName, score: bScore } = b;
if (aScore === bScore) {
// 语文分数相同的情况下,按姓名(字母升序)
return aName < bName ? -1 : aName > bName ? -1 : 0;
} else {
// 按语文分数从大到小(降序)
return bScore - aScore
;
}
});
结果
[
{ name: '慕天恩', sex: '女', age: 18, subject: '语文', score: 99 },
{ name: '杜裕梅', sex: '女', age: 18, subject: '语文', score: 90 },
{ name: '李子舰', sex: '女', age: 18, subject: '语文', score: 90 }
]
其他解法
如果不先分组,是不是可以先把女生过滤出来,再找出有分数低于 90 分的,去除,然后过滤出语文成绩进行排序呢?
const females = array.filter(({ sex }) => sex === "女");
const lowScore = new Set(females.filter(({ score }) => score < 90).map(({ name }) => name));
const result = females.filter(({ name, subject }) => !lowScore.has(name) && subject === "语文")
.sort(({ name: aName, score: aScore }, { name: bName, score: bScore }) => {
return aScore === bScore
? aName < bName ? -1 : aName > bName ? 1 : 0
: bScore - aScore;
});
参考阅读
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容