Home > Software design >  how can i get the string start with lowercase?
how can i get the string start with lowercase?

Time:12-06

const arryStudent = [{
    name: 'faizan',
    rollNo: 11,
    marks: 80
  },
  {
    name: 'irfan',
    rollNo: 12,
    marks: 75
  },
  {
    name: 'Abdullah',
    rollNo: 13,
    marks: 69
  },
  {
    name: 'Asad',
    rollNo: 23,
    marks: 71
  },
  {
    name: 'ali',
    rollNo: 14,
    marks: 71
  }

];

const startsWithA = arryStudent.filter((student) => student.name.startsWith('A'));

console.log(startsWithA);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can do a quick match on the name.

const arryStudent=[{name:"faizan",rollNo:11,marks:80},{name:"irfan",rollNo:12,marks:75},{name:"Abdullah",rollNo:13,marks:69},{name:"Asad",rollNo:23,marks:71},{name:"ali",rollNo:14,marks:71}];

const students = arryStudent.filter(s => s.name.match(/^[a-z]/));

console.log(students);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

This is the way you can check..

    arryStudent.forEach(element => {
        if (element.name[0] == element.name[0].toLowerCase()){
         console.log(`${element.name} startswith lower case`);
      }
        if (element.name[0] == element.name[0].toUpperCase()){
         console.log(`${element.name} startswith Upper case`);
      }

    }); 
  • Related