You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按type_description值升序排序对象中的contact数组?

按type_description字段升序排序数组中每个对象的contact数组

我有如下的data数组,想要将数组中每个对象的contact数组按type_description字段的值进行升序排序,请问该如何实现?

输入数据示例

const data = [
  {
    'name': 'asd',
    'contact': [{
        "country_code": "1",
        "area_code": null,
        "contact_no": "4616984351",
        "email": null,
        "type_description": "Mobile - Primary"
      },
      {
        "country_code": null,
        "area_code": "416",
        "contact_no": "6984351",
        "email": null,
        "type_description": "Landline - Business"
      },
      {
        "country_code": null,
        "area_code": null,
        "contact_no": null,
        "email": "n81ikrfepi@temporary-mail.net",
        "type_description": "Email - Primary"
      }
    ]
  },
  {
    'name': 'dsa',
    'contact': [{
        "country_code": "1",
        "area_code": null,
        "contact_no": "4616984351",
        "email": null,
        "type_description": "Mobile - Primary"
      },
      {
        "country_code": null,
        "area_code": "416",
        "contact_no": "6984351",
        "email": null,
        "type_description": "Landline - Business"
      },
      {
        "country_code": null,
        "area_code": null,
        "contact_no": null,
        "email": "n81ikrfepi@temporary-mail.net",
        "type_description": "Email - Primary"
      }
    ]
  }
];

期望输出结果

const data = [
  {
    'name': 'asd',
    'contact': [{
        "country_code": null,
        "area_code": null,
        "contact_no": null,
        "email": "n81ikrfepi@temporary-mail.net",
        "type_description": "Email - Primary"
      },
      {
        "country_code": null,
        "area_code": "416",
        "contact_no": "6984351",
        "email": null,
        "type_description": "Landline - Business"
      },
      {
        "country_code": "1",
        "area_code": null,
        "contact_no": "4616984351",
        "email": null,
        "type_description": "Mobile - Primary"
      }
    ]
  },
  {
    'name': 'dsa',
    'contact': [{
        "country_code": null,
        "area_code": null,
        "contact_no": null,
        "email": "n81ikrfepi@temporary-mail.net",
        "type_description": "Email - Primary"
      },
      {
        "country_code": null,
        "area_code": "416",
        "contact_no": "6984351",
        "email": null,
        "type_description": "Landline - Business"
      },
      {
        "country_code": "1",
        "area_code": null,
        "contact_no": "4616984351",
        "email": null,
        "type_description": "Mobile - Primary"
      }
    ]
  }
];

实现方案

通过遍历data数组的每个元素,对其contact数组使用sort()方法,结合字符串的localeCompare()函数即可实现需求:

// 遍历data数组中的每个对象
data.forEach(item => {
  // 对当前对象的contact数组按type_description升序排序
  item.contact.sort((a, b) => {
    return a.type_description.localeCompare(b.type_description);
  });
});

关键说明

  • forEach():遍历数组确保每个对象的contact数组都被处理
  • sort():数组排序方法,通过比较函数返回值决定元素顺序:返回负数则a在前,正数则b在前,0则位置不变
  • localeCompare():字符串比较方法,遵循语言环境规则排序,比直接用>或<比较字符串更准确,能适配特殊字符和多语言场景

执行这段代码后,data数组中每个对象的contact数组就会按照type_description字段的值升序排列,得到期望结果。

内容的提问来源于stack exchange,提问作者Arch Cant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 05:21:57