javascript 我能做什么我得到了当我搜索类然后计算有多少学生在课堂上

qlzsbp2j  于 2022-12-25  发布在  Java
关注(0)|答案(4)|浏览(124)

让学生= [{ id:1,name:'aman',class:'10'性别:'male'
},{身份证:2,姓名:“shivani”,班级:“10”性别:“女性”},身份证:2,姓名:“riyan”,班级:“11”性别:“女性”}]

function customFilter(objList, text){
    if(undefined === text || text === '' ) return objList;
    return objList.filter(product => {
        let flag;
        for(let prop in product){
            if(product[prop].toString().indexOf(text)>-1){
                product[prop] = 0
                product[prop]++
                flag = product[prop]
                console.log(flag)

            }
            
          
        }
    return flag;
    });}
    
    console.log( customFilter(student, '10'))

我想当我把10类作为参数传递时,我得到输出10类中有多少学生输出:{班级:10,学生:5 }

xnifntxz

xnifntxz1#

这也可以:

let students = [
  { id: 1, name: "aman", class: "10", gender: "male" },
  { id: 2, name: "shivani", class: "10", gender: "female" },
  { id: 2, name: "riyan", class: "11", gender: "female" },
];

const customFilter = (students, key, value) => {
  const res = { [key]: value, student: 0 };
  students.forEach((student) => {
    if (student[key] === value) {
      res.student += 1;
    }
  });
  return res;
};

console.log(customFilter(students, "class", "10"));

使用Array.prototype.forEach()

2mbi3lxu

2mbi3lxu2#

我没有很好地理解你的问题,但是我假设你想要像{class:10, students: 2}这样的班级中的学生人数

let student = [
    { id:1, name:'aman', class:'10', gender:'male'},
    { id:2, name:'shivani', class:'10', gender:'female' },
    {id:3, name:'riyan', class:'11', gender:'female' }]

function customFilter(objList, text){
    if(undefined === text || text === '' ) return objList;
   const numberOfStudents =  objList.filter(product => {
        let flag;
        for(let prop in product){
            if(product[prop].toString().indexOf(text)>-1){
                product[prop] = 0
                product[prop]++
                flag = product[prop]
                

            }
           
          
        }
    return flag;
    });
     return {class:text, student:numberOfStudents.length }
}
    
    console.log( customFilter(student, '10'))

如果这段代码可以做到这一点,希望它能有所帮助

3phpmpom

3phpmpom3#

代码有一些问题。将class:'10'更改为grade: 10,

  • 不要使用“class”作为变量名.这可能会导致一些错误
  • 缺少一个,
  • 数字不应在引号内,因为数字将存储为string
let student = [
  { id: 1, name: 'aman', grade: 10, gender: 'male'},
  { id: 2, name: 'shivani', grade: 10, gender: 'female' }, 
  { id: 2, name: 'riyan', grade: 11, gender: 'female' },
]

function customFilter(objList, value){
  if(!value || value === '') return objList;

  let count = 0
  objList.forEach(obj => {
    const { grade } = obj;
    if(grade === value){
      count++;
    }
  })
  return {grade: 10, count};
}

console.log(customFilter(student, 10));

forEach可以用来代替filter。它从array的开头循环到结尾

kknvjkwl

kknvjkwl4#

  • 使用.reduce()将所有匹配的对象编组。
/* hits (accumulator) is initially an empty array.
   now (current) is the object of the current iteration. */

array.reduce((hits, now) => { //...
  • 在每次迭代中,获取数组中当前对象的所有值(小写)。
Object.values(now).map(val => val.toLowerCase()) 

/* result of the first object: ["01gn3z1ryjjqhn588ax3bws6qb", "theo bramstom", 
   "genderqueer", "english"] */
  • 如果当前对象的任何值与给定字符串(term)匹配,则将当前对象添加到hits数组中。
if (Object.values(now).map(val => val.toLowerCase()).includes(term.toLowerCase())) {
  hits.push(now);
}
  • 返回对象文本。
{
  "students": /* an array of all matched objects */,
  "total": /* the .length of "students" array */
};

/* To get the answer prompted in OP -- (NOTE: class values are changed for 
   aesthetics) */

const x = dataFilter(students, "Math");
console.log(x.total);
const students=[{id:"01GN3Z1RYJJQHN588AX3BWS6QB",name:"Theo Bramstom",gender:"Genderqueer",class:"English"},{id:"01GN3Z1RYM527HAX56ZN14F0YB",name:"Juli Marsy",gender:"Female",class:"History"},{id:"01GN3Z1RYPYP1FFFEY55T92VX2",name:"Linc Espley",gender:"Non-binary",class:"Math"},{id:"01GN3Z1RYR325M0QETVVPE2N5J",name:"Barbabas Grisley",gender:"Male",class:"Math"},{id:"01GN3Z1RYTXA49SBQYXR9DMC04",name:"Godfree Braybrook",gender:"Male",class:"English"},{id:"01GN3Z1RYVE4N5D16C8QWB1XGF",name:"Jason De Vuyst",gender:"Male",class:"History"},{id:"01GN3Z1RYXY9WXF1Y407HXFYH8",name:"Adler McCanny",gender:"Male",class:"Math"},{id:"01GN3Z1RYY9XV444J0SP5Y0QC2",name:"Noellyn MacMorland",gender:"Genderqueer",class:"Math"},{id:"01GN3Z1RZ0HPQNZ1VKX8ZHA9ZY",name:"Padget Geldeford",gender:"Male",class:"Math"},
{id:"01GN3Z1RZ2DZE92NG42KSGDXN9",name:"Milissent Treby",gender:"Female",class:"Art"}];

const dataFilter = (array, term) => {
  let result = array.reduce((hits, now) => {
    if (Object.values(now).map(val => val.toLowerCase()).includes(term.toLowerCase())) {
      hits.push(now);
    }
    return hits;
  }, []);
  return {"students": result, "total": result.length};
}

console.log(dataFilter(students, "Math"));
console.log(dataFilter(students, "PE"));
console.log(dataFilter(students, "female"));

相关问题