Skip to content

Instantly share code, notes, and snippets.

@mrxf
Created January 30, 2018 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrxf/f27216ab5608df697e5a5e22683e7b4f to your computer and use it in GitHub Desktop.
Save mrxf/f27216ab5608df697e5a5e22683e7b4f to your computer and use it in GitHub Desktop.
compare two differ between two Array
const lastMonthData = [
{
id: 1,
name: "Alex",
salary: 15662
},
{
id: 2,
name: "Tom",
salary: 18672
},
{
id: 3,
name: "Smith",
salary: 14001
}
]
const thisMonthData = [
{
id: 1,
name: "Alex",
salary: 19901
},
{
id: 2,
name: "Tom",
salary: 18672
},
{
id: 3,
name: "Smith",
salary: 11001
}
]
/**
* 只获取薪水增加的员工
* Only get staffs who was increase in salary
**/
const luckyDogs = thisMonthData.filter(tMonth => {
return lastMonthData.filter(lMonth => {
return (tMonth.id === lMonth.id && tMonth.salary <= lMonth.salary);
}).length === 0;
})
/**
* 获取所有调整过员工的信息
**/
const adjustedStaffs = thisMonthData.filter(tMonth => {
return lastMonthData.filter(lMonth => {
return (tMonth.id === lMonth.id && tMonth.salary === lMonth.salary);
}).length === 0;
})
console.log(luckyDogs);
console.log(adjustedStaffs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment