YOSHINO日記

プログラミングに関すること

Ember.js入門5: 配列に使える様々なメソッド

forEach

rubyのeach_with_index

const students = ['Erik', 'Jim', 'Shelly', 'Kate'];

students.forEach(function(item, index) {
  console.log(`Student #${index+1}: ${item}`);
})
Student #1: Erik
Student #2: Jim
Student #3: Shelly
Student #4: Kate

map

rubyのmap

const students = ['Erik', 'Jim', 'Shelly', 'Kate'];

const upperCaseStudents = students.map(function(item){
  return item.toUpperCase();
})

upperCaseStudents.forEach(function(item, index) {
  console.log(`Student #${index+1}: ${item}`);
})
Student #1: ERIK
Student #2: JIM
Student #3: SHELLY
Student #4: KATE

mapBy

rubyにはない。

配列にobjectが格納されている時に、propertyの名前を使って検索することができる。

使いどころがあんまりよくわからん。

const student = EmberObject.extend({
  name: 'Erik Hanchett'
});

const teacher = EmberObject.extend({
  name: 'Jorn P. Smith'
});

const s = student.create();
const t = teacher.create();
const people = [s, t]
console.log(people.mapBy('name'))
["Erik Hanchett", "Jorn P. Smith"]

配列へアクセスしたりpushしたりする。

const students = ['Erik', 'Jim', 'Shelly', 'Kate'];
console.log(students.get('firstObject'));
console.log(students.get('lastObject'));
students.pushObject('Hoge');
console.log(students.get('lastObject'));
Erik
Kate
Hoge

filter

条件に合致するものだけを返す

const nums = [1,23,33,4,5,6,8];

const new_nums = nums.filter(function(item, index, self){
  return item > 10
});

console.log(new_nums);

filterBy

mapByでプロパティの条件までを指定して検索することができる。

const Student = EmberObject.extend({
  name: null,
  age: null
});

const students = [
  Student.create({name:"hoge1", age: 10}),
  Student.create({name:"foo", age: 20}),
  Student.create({name:"hoge2", age: 10}),
  Student.create({name:"moo", age: 30}),
  Student.create({name:"hoge3", age: 10})
];

const students_age10 = students.filterBy('age',10);
students_age10.forEach(function(item,index){
  console.log(item.get('name'));
});

find

最初にマッチするものだけ取り出す。

const nums = [1,23,33,4,5,6,8];

const first_match = nums.find(function(item, index, self){
  return item > 10
});
console.log(first_match)

filterBy

filterの条件が合致するものを1つだけ返すバージョン。

const Student = EmberObject.extend({
  name: null,
  age: null
});

const students = [
  Student.create({name:"hoge1", age: 10}),
  Student.create({name:"foo", age: 20}),
  Student.create({name:"hoge2", age: 10}),
  Student.create({name:"moo", age: 30}),
  Student.create({name:"hoge3", age: 10})
];

const s_10 = students.findBy('age',10);
console.log(s_10.get('name'));

every

配列の要素の全てが条件に合致するならtrueを返す。

const Student = EmberObject.extend({
  name: null,
  age: null
});

const students = [
  Student.create({name:"hoge1", age: 10}),
  Student.create({name:"foo", age: 20}),
  Student.create({name:"hoge2", age: 10}),
  Student.create({name:"moo", age: 30}),
  Student.create({name:"hoge3", age: 10})
];

const res = students.every(function(item,index,self){
  return item.get('age') > 5
});
console.log(res);
true

any

条件に1つでも合致するならtrue

const Student = EmberObject.extend({
  name: null,
  age: null
});

const students = [
  Student.create({name:"hoge1", age: 10}),
  Student.create({name:"foo", age: 20}),
  Student.create({name:"hoge2", age: 10}),
  Student.create({name:"moo", age: 30}),
  Student.create({name:"hoge3", age: 10})
];

const res = students.any(function(item,index,self){
  return item.get('age') > 29
});
console.log(res);