Array.methods

by John Allan

JavaScript

//Array.map
let employees, employeesUpdate;

employees = [
	{
  	name: 'steve',
    salary: 100000
  },
  {
  	name: 'mary',
    salary: 100000
  },
  {
  	name: 'jerry',
    salary: 100000
  },
];

employeesUpdate = employees.map(employee => employee.salary += 10000);

console.log(employeesUpdate[1]);

//Array.filter

let lineup, entrants;

lineup = [
	{ name: 'steve', age: 21},
  { name: 'albert', age: 15},
  { name: 'martin', age: 32}
];

entrants = lineup.filter(applicant => applicant.age >= 19);

console.log(entrants);

//Array.forEach

let cats;

cats = ['tabby', 'manx', 'persian'];

cats.forEach(cat => console.log(cat));

//Array Unique
let ages;

ages = [5,6,7,8,3,25,35,7,63,5,78,9,1,3,25];

Array.prototype.unique = function () {
	let u, check;
  
  check = {};
  
  u = this.filter(i => {
  	if (check[i]) return false;
    return check[i] = true;
  });
  
  return u;
}

Array.prototype.uniqueSet = function () {
  return new Set(this);
}; 

console.log(ages.unique());
console.log(ages.uniqueSet());