二.Loops & Iteration
2429 ワード
every()メソッド
array.every(function(currentValue,index,arr), thisValue)
function isBigEnough(element){
return (element >=10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
ろ過方法
var a = [1, 2, 3, 7,4].filter(function(value){
return value > 4
})
console.log(a)//[7]
forEach()メソッド
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 4,9].forEach(logArrayElements);
// a[0] = 2
// a[1] = 5
// a[2] = 4
// a[3] = 9
map()メソッド
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
/* roots [1, 2, 3], numbers [1, 4, 9] */
some()メソッド
var a = [1, 2, 3, 7,4].some (function(value){
return value > 8
})
console.log(a)//false
a = [1, 2, 3, 9,4].some (function(value){
return value > 8
})
console.log(a)//true
forサイクル for( var index = 0; index < array.length; i ++){}
for-inサイクル var obj = {a:1, b:2, c:3};
for (var prop in obj) {
console.log("obj." + prop + " = " + obj[prop]);
}
// :
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"
for-ofサイクル
for( var index = 0; index < array.length; i ++){}
var obj = {a:1, b:2, c:3};
for (var prop in obj) {
console.log("obj." + prop + " = " + obj[prop]);
}
// :
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"
for-ofサイクル
let iterable = [10, 20, 30];
for (let value of iterable) {
console.log(value);
}
// 10
// 20
// 30