Javascript Typescript TypeError:无法读取属性';收集';未定义的

Javascript Typescript TypeError:无法读取属性';收集';未定义的,javascript,angular,typescript,Javascript,Angular,Typescript,我在尝试推送对象时遇到一个未定义“collection”的错误 groupShortlists(lists: ProductShortlist[]) { const grouped = lists.reduce((groupedList, list) => { groupedList[list.collection] = [...groupedList[list.collection] || [], list]; return groupedList

我在尝试推送对象时遇到一个未定义“collection”的错误

groupShortlists(lists: ProductShortlist[]) {
    const grouped = lists.reduce((groupedList, list) => {
        groupedList[list.collection] = [...groupedList[list.collection] || [], list];
        return groupedList;
      }, {});

    return Object.keys(grouped).map(key => grouped[key]);
  }


this.shortlistsCategory = [];
this.groupShortlists(this.shortLists).map((item, key) => {
      this.shortlistsCategory.push({
        collection: item[key].collection,
        list: [],
      });
    });
确切的错误是:

TypeError:无法读取未定义的属性“collection”


这是使用推的正确方法吗。任何帮助都将不胜感激D在这种情况下,
已经是
短名单
的索引
处的值。您可以执行
项.collection
短名单[key]。collection
,但不能同时执行两者

另外,使用
.map
已经返回指定的对象,因此不需要使用
.map
.push
(这不是语义)

下面是将短名单类别设置为
.map
返回值的示例

this.shortlistsCategory=this.groupshortlist(this.shortlist)
.map(项目=>{
返回{
集合:item.collection,
名单:[]
};
});

链接到
.map
文档:

在map函数中,第一个参数将是该数组的单个obj/项,第二个参数将是
索引
。因此,请确保项目[0]存在。
groupshortlist
的作用是什么?你能包含一个代码片段吗?请更新。我介绍了groupShortlist的功能。在查看了groupShortlist方法的代码片段后,返回的数组可能有嵌套数组(这可能是个问题)。你能举一个
产品短名单的例子吗?