JavaScript

[ES6 JS]Array Helper Methods

Array Helper Methods

forEach

var colors = ['red', 'blue', 'green'];

for (var i = 0; i < colors.length; i++) {
  console.log(colors[i]);
}

+ES+6+

let colors = ['red', 'blue', 'green'];

colors.forEach(function(color) {
  console.log(color);
});

map

var numbers = [1, 2, 3];
var doubledNumbers = [];

for (var i = 0; i < numbers.length; i++) {
  doubledNumbers.push(numbers[i] * 2);
}

console.log(doubledNumbers);

+ES+6+

let numbers = [1, 2, 3];
let doubledNumbers = numbers.map(function(number) {
  return number * 2;    
});

console.log(doubledNumbers);

pluck implementing

function pluck(array, prop) {
  return array.map((el) => {
    return el[prop];
  });
}

filter

var products = [
  { name: 'banana', type: 'fruit' },
  { name: 'cucumber', type: 'vegetable' },
  { name: 'orange', type: 'fruit' },
  { name: 'celery', type: 'vegetable' }
];
var filteredProducts = [];

for (var i = 0; i < products.length; i++) {
  if (products[i].type === 'fruit') {
    filteredProducts.push(products[i]);
  }
}

console.log(filteredProducts);

+ES+6+

let products = [
  { name: 'banana', type: 'fruit' },
  { name: 'cucumber', type: 'vegetable' },
  { name: 'orange', type: 'fruit' },
  { name: 'celery', type: 'vegetable' }
];

filteredProducts = products.filter(function(product) {
  return product.type === 'fruit';
});

console.log(filteredProducts);

find

var users = [
  { name: 'Jill'},
  { name: 'Alex'},
  { name: 'Mark'}
];
var user;

for (var i = 0; i < users.length; i++) {
  if (users[i].name === 'Alex') {
    user = users[i];
    break;
  }
}

console.log(user);

+ES+6+

const users = [
  { name: 'Jill'},
  { name: 'Alex'},
  { name: 'Mark'}
];

let user = users.find((user) => {
  return user.name === 'Alex';
});

console.log(user);
function Car(model) {
  this.model = model;
}

const cars = [
  new Car('AAA'),
  new Car('BBB'),
  new Car('CCC')
];

let car = cars.find((car) => {
  return car.model === 'CCC';
});

console.log(car);

---
class Car {
  constructor(model) {
    this.model = model;
  }
}

const cars = [
  new Car('AAA'),
  new Car('BBB'),
  new Car('CCC')
];

let car = cars.find((car) => {
  return car.model === 'CCC';
});

console.log(car);
let posts = [
  {id: 1, title: 'New' },
  {id: 2, title: 'Old' },
];

let comment = { postId: 1, content: 'GGG'};

function postForComment(posts, comment) {
  return posts.find((post) => {
    return post.id === comment.postId;
  });
}

console.log(postForComment(posts, comment))
const ladders = [
  { id: 1, height: 20 },
  { id: 3, height: 25 }
];

function findWhere(array, criteria) {
  return array.find((el) => {
    return Object.keys(criteria).find((key) => {
      return el[key] === criteria[key]; 
    });
  });
}

console.log(findWhere(ladders, { height: 25 }));

every, some

var comps = [
  { name: 'AAA', ram: 24 },
  { name: 'BBB', ram: 4 },
  { name: 'CCC', ram: 32 }
];

var compAllCanRun = true;
var compSomeCanRun = false;

for (var i = 0; i < comps.length; i++) {
  var comp = comps[i];

  if (comp.ram > 16) {
    compAllCanRun = false;
  } else {
    compSomeCanRun = true;
  }
}

console.log(compAllCanRun);
console.log(compSomeCanRun);

+ES+6+

const comps = [
  { name: 'AAA', ram: 24 },
  { name: 'BBB', ram: 4 },
  { name: 'CCC', ram: 32 }
];

let compAllCanRun = comps.every((comp) => {
  return comp.ram > 16
});

let compSomeCanRun = comps.some((comp) => {
  return comp.ram > 16
});

console.log(compAllCanRun);
console.log(compSomeCanRun);
class FormField {
  constructor(value) {
    this.value = value;
  }
  validate() {
    return this.value.length > 0;
  }
}

let username = new FormField("user");
let password = new FormField("pass");
let birthday = new FormField("19xx/xx/xx");

const fields = [username, password, birthday];

let formIsValid = fields.every(field => {
  return field.validate();
});

console.log(formIsValid);

reduce

var numbers = [1, 2, 3];
var sum = 0;

for (var i = 0; i < numbers.length; i++) {
  sum += numbers[i];
}

console.log(sum);

+ES+6+

let numbers = [1, 2, 3];
let sum = 0;

sum = numbers.reduce((sum, number) => {
        return sum + number;
      }, 0);

console.log(sum);
let priColors = [
  { color: 'red'},
  { color: 'submarine'},
  { color: 'blue'}
];

colors = priColors.reduce((prev, priColor) => {
  prev.push(priColor.color);
  return prev;
}, []);

console.log(colors);
function balanced(str) {
  return !str.split("").reduce((prev, char) => {
    if (prev < 0) { return prev; }
    if (char === "(") { return ++prev; }
    if (char === ")") { return --prev; }
    return prev
  }, 0);
}

console.log(balanced("(()"));
console.log(balanced("(())"));
console.log(balanced("aaa(())"));
console.log(balanced(")("));
console.log(balanced("aaa))(())"));