Skip to content

Instantly share code, notes, and snippets.

@Andaeiii
Last active April 1, 2022 15:41
Show Gist options
  • Save Andaeiii/9b77dc76822adefce445c88fb40b8a41 to your computer and use it in GitHub Desktop.
Save Andaeiii/9b77dc76822adefce445c88fb40b8a41 to your computer and use it in GitHub Desktop.
..brief insight into the usage of common JS ES6 methods
var arr = String('aminu;ocholi;oketta;odinma;mohammed;fidelis;james;brown;').split(';');
var students = [
{ name: 'aminu', age: 9, class: 'primary 6' },
{ name: 'delilah', age: 84, class: 'primary 6' },
{ name: 'mary', age: 19, class: 'primary 6' },
{ name: 'sujim', age: 12, class: 'primary 6' },
{ name: 'Mohammed', age: 29, class: 'primary 6' },
{ name: 'Tango', age: 84, class: 'primary 6' },
{ name: 'Maimuna', age: 69, class: 'primary 6' },
{ name: 'tobi', age: 3, class: 'primary 6' }
];
var scores = [34, 567, 323, 11, 665, 78, 3, 6, 88, 2];
var sorted = scores.sort((a, b) => b - a);
var filtered = students.filter((a) => a.age < 20).sort((a, b) => a.age - b.age); using the sort.
//when sorting, keep in mind that ...
sort((a, b) => a - b) // for ascending... from 0 to 100
sort((a, b) => b - a) // for descending... from 100 to 0
console.log(filtered)
var totalAges = students.reduce((a, c) => a + c.age, 0); //initial value of 0
console.log(totalAges)
//for Object destructuring.. and works same for array....
const {name, age} = students[2];
console.log(`Student 2 = ${name} and he is ${age}yrs old`); // Student 2 = mary and he is 19yrs old
//then for using the spread operator...
let newStudent = {name:'Tolani', age:13, class:'primary 4'};
allStudents = {...students, newStudent}; //spread the object and lay its elements then include an extra element.
// this code is the same as ... saying.
students.push(newStudent); //gives yout he same output...
/*
where you can push a new code into a new array, you can just spread the elements out and cast them either as an object or an array...
as the case may be...
*/
//forEach
students.forEach(student => {
//do whatever you want with each student...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment