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);
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`);
}
});
Write a function that filters out the names based on the letter.
const students=[{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}]; // Write a function that accepts the data, // and the letter you're looking for function find(arr, letter) { // Always lowercase the letter const lc = letter.toLowerCase(); // And then filter out the names that match return arr.filter(student => { return student.name.toLowerCase().startsWith(lc); }); } console.log(find(students, 'A'));
You can use startsWith
twice like:
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') || student.name.startsWith('a')); console.log(startsWithA);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.