Home > Software design >  what can i do i got out when i am searching class then count how many student in class
what can i do i got out when i am searching class then count how many student in class

Time:12-25

let student = [{ id:1, name:'aman', class:'10' gender:'male'

},{ id:2, name:'shivani', class:'10' gender:'female' }, id:2, name:'riyan', class:'11' gender:'female' }]

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'))

i want to as like when i am pass class 10 as argument then i got output how many student in class 10 out put: {class:10,stduent:5 }

CodePudding user response:

This would also work:

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"));

Using Array.prototype.forEach()

CodePudding user response:

I didn't get your question well, but I assumed you want number of student in a class like this {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'))

If that's the case this code will do , hope it helps

CodePudding user response:

There are few problems with the code. change class:'10' to grade: 10,.

  • don't use "class" as a variable name. might cause a few errors
  • There is a missing ,
  • numbers shouldn't be inside quotes because the number will be stored as a 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));

and forEach can be used instead of filter. It loops from start to end of an array

CodePudding user response:

  • Use .reduce() to group all objects that match.

    /* hits (accumulator) is initially an empty array.
       now (current) is the object of the current iteration. */
    
    array.reduce((hits, now) => { //...
    
  • On each iteration, get all of the current object's values (in lower case) in an array.

    Object.values(now).map(val => val.toLowerCase()) 
    
    /* result of the first object: ["01gn3z1ryjjqhn588ax3bws6qb", "theo bramstom", 
       "genderqueer", "english"] */
    
  • If any of the values of the current object matches the given string (term), add the current object to the hits array.

    if (Object.values(now).map(val => val.toLowerCase()).includes(term.toLowerCase())) {
      hits.push(now);
    }
    
  • An object literal is returned.

    {
      "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"));

  • Related