过滤数组并添加属性

时间:2018-11-10 20:52:23

标签: javascript

我有一个数组,其中有一些项目的每周进度。

在当前的一周(时间戳为1529539200或日期为06/21/2018)中,我获得了3个项目的信息。项目20、21和22。

在另一周,即当前06/14/2018的前一周,只有2个项目的信息。因为项目22不存在。

progressList = [
  // current 06/21/2018
  {
    project: 20, 
    timestamp: 1529539200, // seconds
    current: true,
    progress: 90
  },
  {
    project: 21, 
    timestamp: 1529539200,
    current: true,
    progress: 70
  },
  {
    project: 22,
    timestamp: 1529539200,
    current: true,
    progress: 100
  },
  // 06/14/2018
  {
    project: 20, 
    timestamp: 1528934400, // a week before (1529539200 - 604800)
    current: false,
    progress: 80
  },
  {
    project: 21, 
    timestamp: 1528934400,
    current: false,
    progress: 50
  }  
]

我需要的是当前项目的列表,但是要包含一个新的属性“ increase”(增加),该属性等于当前项目进度与前一周进度之间的差。

例如,我应该得到这样的东西:

currentProgressList = [
  {
    project: 20, 
    timestamp: 1529539200,
    current: true,
    progress: 90,
    increase: 10 // difference
  },
  {
    project: 21, 
    timestamp: 1529539200,
    current: true,
    progress: 70,
    increase: 20
  },
  {
    project: 22,
    timestamp: 1529539200,
    current: true,
    progress: 100,
    increase: 0 // 
  }
]

我想通过current属性过滤项目可能很简单:

listadoprogresss.filter(p => {
  return p.current === true
})

但是,我不知道如何根据我的需要添加increase属性。

预先感谢

2 个答案:

答案 0 :(得分:2)

const progressList = [
  { project: 20, timestamp: 1529539200, current: true, progress: 90 },
  { project: 21, timestamp: 1529539200, current: true, progress: 70 },
  { project: 22, timestamp: 1529539200, current: true, progress: 100 },
  { project: 20, timestamp: 1528934400, current: false, progress: 80 },
  { project: 21, timestamp: 1528934400, current: false, progress: 50 }  
]

function getCurrentProgressList(progressList) {

  const projects = [];
  const indexes = {};
  
  for(const Project of progressList) {
    const index = indexes[Project.project];
    const CurrentProject = projects[index];

    if (Project.current) {
      indexes[Project.project] = projects.length;
      projects.push({ ...Project, increase: 0 });
    } else if (index !== undefined) {
      const a = CurrentProject.progress;
      const b = Project.progress;
      CurrentProject['increase'] = a - b;
    }
  }
  
  return projects;
}

console.log(getCurrentProgressList(progressList));

答案 1 :(得分:2)

这是一个可能的解决方案,您可以调整 checkIncrease 以适合您的解决方案。但运行代码片段

let checkIncrease = function(list, p) {
    let prevProgress = list.find(np => np.project === p.project && !np.current)
    var increase = 0;
    // confirm that its defined
    if (prevProgress) increase = p.progress - prevProgress.progress
    
    // create a clone and return
    return  { project: p.project, timestamp: p.timestamp, current: p.current, progress: p.progress, increase: increase }
}

let getProgressIncrease = function(progressList) {
  return  progressList
  .filter(p => p.current)
  .map(p => checkIncrease(progressList, p))
}


var progressList = [
  // current 06/21/2018
  {
    project: 20, 
    timestamp: 1529539200, // seconds
    current: true,
    progress: 90
  },
  {
    project: 21, 
    timestamp: 1529539200,
    current: true,
    progress: 70
  },
  {
    project: 22,
    timestamp: 1529539200,
    current: true,
    progress: 100
  },
  // 06/14/2018
  {
    project: 20, 
    timestamp: 1528934400, // a week before (1529539200 - 604800)
    current: false,
    progress: 80
  },
  {
    project: 21, 
    timestamp: 1528934400,
    current: false,
    progress: 50
  }  
]

// try it out
let result = getProgressIncrease(progressList)
console.log(result)