javascript - Javascript - 如何将日期传递给getFullYear ()?

实现getMenCountByYear函数,该函数接收用户列表并返回一个对象,该对象中的键是出生年份,值是该年出生的男性人数。我做错什么了?


const users = [


 { name: 'Bronn', gender: 'male', birthday: '1973-03-23' },


 { name: 'Reigar', gender: 'male', birthday: '1973-11-03' },


 { name: 'Eiegon', gender: 'male', birthday: '1963-11-03' },


 { name: 'Sansa', gender: 'female', birthday: '2012-11-03' },


 { name: 'Jon', gender: 'male', birthday: '1980-11-03' },


 { name: 'Robb', gender: 'male', birthday: '1980-05-14' },


 { name: 'Tisha', gender: 'female', birthday: '2012-11-03' },


 { name: 'Rick', gender: 'male', birthday: '2012-11-03' },


 { name: 'Joffrey', gender: 'male', birthday: '1999-11-03' },


 { name: 'Edd', gender: 'male', birthday: '1973-11-03' },


];



getMenCountByYear(users);


// {


// 1973: 3,


// 1963: 1,


// 1980: 2,


// 2012: 1,


// 1999: 1,


// };



我的代码


const users = [


 { name: 'Bronn', gender: 'male', birthday: '1973-03-23' },


 { name: 'Reigar', gender: 'male', birthday: '1973-11-03' },


 { name: 'Eiegon', gender: 'male', birthday: '1963-11-03' },


 { name: 'Sansa', gender: 'female', birthday: '2012-11-03' },


 { name: 'Jon', gender: 'male', birthday: '1980-11-03' },


 { name: 'Robb', gender: 'male', birthday: '1980-05-14' },


 { name: 'Tisha', gender: 'female', birthday: '2012-11-03' },


 { name: 'Rick', gender: 'male', birthday: '2012-11-03' },


 { name: 'Joffrey', gender: 'male', birthday: '1999-11-03' },


 { name: 'Edd', gender: 'male', birthday: '1973-11-03' },


];



const getMenCountByYear = (users) => {



 const men = users.filter((users) => users.gender == 'male');


 const years = users.map((users) => new Date(users.birthday));


 const yer = years.getFullYear();



 return dates = (acc, year) => {


 if(_.has(year, acc)){


 acc[year] =+ 1;


 } else{ 


 acc[year] = 1;


 }


 return acc;


 };



 const userByYear = yer.reduce(dates, []);


 console.log(yer);


}



console.log(getMenCountByYear(users));


export default getMenCountByYear(users);



时间:

我认为这应该有效:


 const yer = years.map((years)=>years.getFullYear());



你可以尝试这个代码


function getMenCountByYear (users, gender = 'male') {


 return users.filter(user => user.gender === gender)


 .reduce((result, user) => {


 const year = new Date(user.birthday).getFullYear();


 return {...result, [year]: (result[year] || 0) + 1 };


 }, {});


}



getMenCountByYear(users);



//{1963: 1, 1973: 3, 1980: 2, 1999: 1, 2012: 1}



getMenCountByYear(users, 'female');



// {2012: 2}



您可以使用更通用的方法,并简化为


const persons = [


 { name: 'Bronn', gender: 'male', birthday: '1973-03-23' },


 { name: 'Reigar', gender: 'male', birthday: '1973-11-03' },


 { name: 'Eiegon', gender: 'male', birthday: '1963-11-03' },


 { name: 'Sansa', gender: 'female', birthday: '2012-11-03' },


 { name: 'Jon', gender: 'male', birthday: '1980-11-03' },


 { name: 'Robb', gender: 'male', birthday: '1980-05-14' },


 { name: 'Tisha', gender: 'female', birthday: '2012-11-03' },


 { name: 'Rick', gender: 'male', birthday: '2012-11-03' },


 { name: 'Joffrey', gender: 'male', birthday: '1999-11-03' },


 { name: 'Edd', gender: 'male', birthday: '1973-11-03' } ];



console.log(`male`);


console.log(getCountByYear(persons, v => v.gender === `male`));


console.log(`female`);


console.log(getCountByYear(persons, v => v.gender === `female`));


console.log(`all`);


console.log(getCountByYear(persons));



function getCountByYear(users, filter = v => v) {


 return users


 .filter( filter )


 // no need for Date conversion


 .map( user => user.birthday.slice(0, 4) )


 .reduce( (acc, val) => ( {...acc, [val]: (acc[val] || 0) + 1 } ), {});


}
.as-console-wrapper { top: 0; max-height: 100% !important; }

...