开发者问题收集

对嵌套数组中的对象进行排序返回未定义

2022-02-02
152

如果 display_order 为空,我尝试按标题对“exhibitors”数组中的对象进行排序。但是当我尝试这样做时,我收到错误:TypeError:无法分配给对象“[object Array]”的只读属性“0”。是不是因为我尝试排序的数组太深了?

  let sortedTiers = [...exhibitorTiers]
    .sort((a, b) => {
      return a.order - b.order;
    })
    .map((tier) => {
      tier.exhibitors.sort((a, b) => a.title.localeCompare(b.title));
      return tier;
    });



The data structure is below

[
    {
        "exhibitor_tier_id": 269,
        "tier_name": ";ll",
        "order": 1,
        "color": null,
        "type": "expo",
        "exhibitors": [
            {
                "exhibitor_id": 6218,
                "title": "Audax Group",
                "event_stream_id": null,
                "display_order": 1,
            },
            {
                "exhibitor_id": 6220,
                "title": "Apex Group",
                "display_order": 5,

            }
        ]
    },
    {
        "exhibitor_tier_id": 237,
        "tier_name": ";lkj;klj",
        "order": 2,
        "color": null,
        "type": "expo",
        "exhibitors": [
            {
                "exhibitor_id": 6224,
                "title": "EnCap Investments",
                "display_order": null,

            },
            {
                "exhibitor_id": 6226,
                "title": "Preqin",
                "display_order": null,

            },
            {
                "exhibitor_id": 6229,
                "title": "HKVCA",
                "display_order": null,

            },
            {
                "exhibitor_id": 6232,
                "title": "SVCA",
                "display_order": null,
            }
        ]
    }
]```
2个回答

display_order id 不为 null 时,您不会返回任何内容。尝试此操作

var arraytosort = orginalArray.slice();

      arraytosort.sort((a, b) => {
        if (a.display_order === null && b.display_order != null) {
          return -1;
        }else if (a.display_order != null && b.display_order === null) {
          return 1;
        }else if (a.display_order != null && b.display_order != null) {
          return a.display_order - b.display_order;
        }else{
          return a.title.localeCompare(b.title)
        }

      });
    })

orginalArray = arraytosort 
JaivBhup
2022-02-02

开始吧。删除 slice 并比较 display_order (如果存在)

const tiers = [{
    "exhibitor_tier_id": 269,
    "tier_name": ";ll",
    "order": 1,
    "color": null,
    "type": "expo",
    "exhibitors": [{
        "exhibitor_id": 6218,
        "title": "Audax Group",
        "event_stream_id": null,
        "display_order": 1,
      },
      {
        "exhibitor_id": 6220,
        "title": "Apex Group",
        "display_order": 5,

      }
    ]
  },
  {
    "exhibitor_tier_id": 237,
    "tier_name": ";lkj;klj",
    "order": 2,
    "color": null,
    "type": "expo",
    "exhibitors": [{
        "exhibitor_id": 6224,
        "title": "EnCap Investments",
        "display_order": null,

      },
      {
        "exhibitor_id": 6226,
        "title": "Preqin",
        "display_order": null,

      },
      {
        "exhibitor_id": 6229,
        "title": "HKVCA",
        "display_order": null,

      },
      {
        "exhibitor_id": 6232,
        "title": "SVCA",
        "display_order": null,
      }
    ]
  }
];

tiers
  .sort((a, b) => a.order - b.order)
  .forEach(
    tier => tier.exhibitors.sort(
      (a, b) => (~~a.display_order - ~~b.display_order) || a.title.localeCompare(b.title)
    )
  );

console.log(tiers);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Kosh
2022-02-02