Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript/Javascript从具有重复项的数组创建数组,并使用剩余属性创建嵌套数组_Javascript_Typescript - Fatal编程技术网

Typescript/Javascript从具有重复项的数组创建数组,并使用剩余属性创建嵌套数组

Typescript/Javascript从具有重复项的数组创建数组,并使用剩余属性创建嵌套数组,javascript,typescript,Javascript,Typescript,我需要从一个可能具有重复“show”属性的数组创建一个新数组。我只需要1个重复的“show:”,但将所有其他属性保留为数组对象中的“performances:”数组。 示例阵列: const arr1=[ { 类别:“卡巴莱酒店”, 执行日期:“2020年12月2日”, 表演时间:“下午7:00”, 展示:{ showId:6055,预售:,类别ID:31, showDesc:“把你的期望留在门口,作为Tony Awa…pearance,今年夏天在下面的Feinstein's/54!”, 节目

我需要从一个可能具有重复“show”属性的数组创建一个新数组。我只需要1个重复的“show:”,但将所有其他属性保留为数组对象中的“performances:”数组。 示例阵列:

const arr1=[
{
类别:“卡巴莱酒店”,
执行日期:“2020年12月2日”,
表演时间:“下午7:00”,
展示:{
showId:6055,预售:,类别ID:31,
showDesc:“把你的期望留在门口,作为Tony Awa…pearance,今年夏天在下面的Feinstein's/54!

”, 节目名称:“杰里米·乔丹” }, 韦纽伊德:1534 }, { 类别:“卡巴莱酒店”, 执行日期:“2020年2月3日”, 表演时间:“晚上9:30”, 展示:{ showId:5953,预售:,类别:31, showDesc:“每个人都有梦想,我们担心这些梦想会从我们身边溜走……行为随时都会发生变化。

”, showName:“我希望:可以扮演的角色” }, 韦纽伊德:1534 }, { 类别:“卡巴莱酒店”, 执行日期:“2020年2月1日”, 表演时间:“晚上9:30”, 展示:{ showId:6103,预售:,类别ID:31, showDesc:“在“反常的星期五”事件中,当前和…的行为随时可能发生变化。

”, showName:“切换:当前和以前的百老汇儿童交易场所” }, 韦纽伊德:1534 }, { 类别:“卡巴莱酒店”, 执行日期:“2020年2月13日”, 表演时间:“下午7:00”, 展示:{ showId:6055,预售:,类别ID:31, showDesc:“把你的期望留在门口,作为Tony Awa…pearance,今年夏天在下面的Feinstein's/54!

”, 节目名称:“杰里米·乔丹” }, 韦纽伊德:1534 }, { 类别:“卡巴莱酒店”, 执行日期:“2020年2月1日”, 表演时间:“下午7:00”, 展示:{ showId:6400,预售:,类别ID:31,
showDesc:“Broadway mainstayChristine Andreas
reduce
对于这类事情非常有用,但实际上这只是一个循环。试着想象一下你将如何手动完成这项工作,比如说用一堆文件。首先是将文件分组成一堆,在同一个节目中(您可以通过
showId
来判断这一点):

相当于
减少
(实际上没有任何改进):

现在,将这些分组转换为数据结构变得更加简单:

const arr2 = performancesByShow.values().map(performances => ({
  show: performances[0].show,
  performances: performances.map(({ category, perfDate, perfTime, venueId }) => ({
    category, perfDate, perfTime, venueId 
  }))
}));

现在是什么问题?请展示您的尝试和困难是什么?您不能只说:“我需要这个”,您应该告诉自己是什么阻止了您这样做谢谢您的解释。我在等待响应时使用了.reduce()非常接近。
const performancesByShow = arr1.reduce((performancesByShow, performance) => {
  const { showId } = performance.show;
  const performances = performancesByShow.get(showId);
  if (!performances) {
    performances = [];
    performancesByShow.set(showId, performances);
  }
  performances.push(performance);
  return performancesByShow;
}, new Map());
const arr2 = performancesByShow.values().map(performances => ({
  show: performances[0].show,
  performances: performances.map(({ category, perfDate, perfTime, venueId }) => ({
    category, perfDate, perfTime, venueId 
  }))
}));