datetype.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //string
  2. var studentName = "xiaoming";
  3. //number
  4. var studentAge = 18;
  5. //Boolean
  6. var isChecked = true;
  7. //Date
  8. var now = new Date();
  9. //Array
  10. var studentList1 = [
  11. { name: "xiaoming", age: 18, isChecked: true },
  12. { name: "xiaowang'", age: 18, isChecked: true },
  13. ];
  14. var studentnew = {
  15. name: "xiaoming",
  16. age: 18,
  17. isChecked: false,
  18. works: ["yuwen"],
  19. };
  20. studentList1.push(studentnew);
  21. var task1 = { progress: 1, complete: true, errorMsg: "youwenti" };
  22. /**
  23. * 统计人数 没有接口表示
  24. * @param studentList{Array<StudentInt} 学生列表
  25. * @returns
  26. */
  27. // let checkList = studentList.map(item=>item.isChecked ? 1:0)
  28. // let count = 0;
  29. // checkList.forEach(check=>{
  30. // if(check==1)
  31. // count ++
  32. // })
  33. // let checkList = studentList.map((item): 1 | 0 => (item.isChecked ? 1 : 0));
  34. // let count = 0;
  35. // checkList.forEach((checked) => {
  36. // if (checked == 1) count++;
  37. // });
  38. // 使用reduce方法直接累加isChecked为true的学生数量
  39. function checkCount1(studentList) {
  40. return studentList.reduce(function (count, student) {
  41. return count + (student.isChecked ? 1 : 0);
  42. }, 0);
  43. }
  44. /**
  45. * 统计人数 用函数接口表示
  46. * @param studentList{Array<StudentInt} 学生列表
  47. * @returns
  48. */
  49. var checkCount = function (studentList) {
  50. var checkList = studentList.map(function (item) { return (item.isChecked ? 1 : 0); });
  51. var count = 0;
  52. checkList.forEach(function (checked) {
  53. if (checked === 1)
  54. count++;
  55. });
  56. return count;
  57. };
  58. var count = checkCount(studentList1);
  59. console.log(count);