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,
  },
];
阅读 2.7k
6 个回答

先解题

注:题上说 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;
    });

参考阅读

啰嗦了一点,但好懂

const result = array.filter((student) => {
  const isFemale = student.sex === "女"; //筛选出所有女同学,
  const isAllScoresAbove90 = array.filter(
    (s) => s.name === student.name && s.score >= 90
  ).length === 3; //你只有三门课,所以只要当前同学的所有课目都是90分以上返回的数组才有三个元素
  return isFemale && isAllScoresAbove90;
})

//仅仅是为了方便后面排序
Array.prototype.yunwen=function(){
  return this.find(s=>{
    return s.subject==="语文"
     
  })
}

function groupBy(list, keyGetter) {
    const map = new Map();
    list.forEach((item) => {
         const key = keyGetter(item);
         const collection = map.get(key);
         if (!collection) {
             map.set(key, [item]);
         } else {
             collection.push(item);
         }
    });
    return map;
}

const result2 = groupBy(result,s => s.name);//换名字组合在一起方便排序

const res=new Map([...result2.entries()].sort((a,b)=>{
  return b[1].yunwen().score-a[1].yunwen().score //根据语文排序
}))
 
//回复原样式数组
const res1 = Array.from(res.values());
console.log(res1.flat());
本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。

image.png

 // 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
      }
    })
本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。

可以借助 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 这样排序。具体可以通过这个问答了解详细原因 👉 明明‘a'大于'A',为什么localeCompare返回-1?

相关链接

Map - JavaScript | MDN
Array.prototype.filter() - JavaScript | MDN
Array.prototype.sort() - JavaScript | MDN
String.prototype.localeCompare() - JavaScript | MDN

本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。
arr.filter(v => v.sex == '女' && v.score >= 90).
    sort((a, b) => {
      if (a.subject == '语文' && b.subject == '语文') return b.score - a.score;
      else return -1
    })
本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。
function filter(list, callback, key) {
    var ret = [], group = {};
    for (var i = 0; i < list.length; ++i) {
        var item = list[i], id = item[key];
        group[id] = (group[id] || []).concat([item]);
    }
    outer: for (var prop in group) {
        var chunk = group[prop];
        for (var i = 0; i < chunk.length; ++i) {
            if (!callback(chunk[i], i, list)) continue outer;
        }
        ret.push.apply(ret, chunk);
    }
    return ret;
}
var result = filter(array, function (value) {
    return value.sex === "女" && value.score >= 90;
}, "name").sort(function (a, b) {
    var ret = 0;
    if (a.subject === "语文" && b.subject === a.subject) {
        ret += a.score - b.score;
    }
    return ret || (a.name.localeCompare(b.name));
});
console.log(result);
推荐问题
宣传栏