JS reduce: object 累加器标题
JS reduce: object accumulator titles
我目前正在试验 reduce 函数,想知道是否可以将累加器的键名定制为特定值?
例如,下面的代码 returns {16 years of experience: ... } 但我希望 return 结果分类如下:
Results wanted in this format:
{ Over 10 years:
16: {
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{name: "Company Two", category: "Retail", start: 1992, end: 2008}
},
20: {
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{name: "Company Two", category: "Retail", start: 1992, end: 2008}
}
...
} etc...
const companies = [
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const industryExeperience = (obj, keyStart, keyEnd)=>{
return obj.reduce((exp, curObj)=>{
let differences = curObj[keyEnd] - curObj[keyStart];
console.log(differences)
if(differences > 10){
exp[differences + ' years of experience'] = curObj
}
return exp
},{})
}
console.log(industryExeperience(companies, 'start', 'end'))
要按年份分组,请使用差异作为键,使用数组作为值。
将每个项目推入或连接到现有数组或根据需要添加新项目
const companies = [
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const grouped = companies.reduce((a,c)=>{
const diff = c.end - c.start;
a[diff] = (a[diff] || []).concat(c)
return a;
},{})
console.log(grouped)
我目前正在试验 reduce 函数,想知道是否可以将累加器的键名定制为特定值?
例如,下面的代码 returns {16 years of experience: ... } 但我希望 return 结果分类如下:
Results wanted in this format:
{ Over 10 years:
16: {
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{name: "Company Two", category: "Retail", start: 1992, end: 2008}
},
20: {
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{name: "Company Two", category: "Retail", start: 1992, end: 2008}
}
...
} etc...
const companies = [
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const industryExeperience = (obj, keyStart, keyEnd)=>{
return obj.reduce((exp, curObj)=>{
let differences = curObj[keyEnd] - curObj[keyStart];
console.log(differences)
if(differences > 10){
exp[differences + ' years of experience'] = curObj
}
return exp
},{})
}
console.log(industryExeperience(companies, 'start', 'end'))
要按年份分组,请使用差异作为键,使用数组作为值。 将每个项目推入或连接到现有数组或根据需要添加新项目
const companies = [
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const grouped = companies.reduce((a,c)=>{
const diff = c.end - c.start;
a[diff] = (a[diff] || []).concat(c)
return a;
},{})
console.log(grouped)