JavaScriptのいくつかの常用方法の一つ.
11844 ワード
, JavaScript , , , 。 。
このブログでは、まずこれらの方法の用途を説明します.splice()、push()、pop()、Shift()、unshift()、hasOwnProperty()、map()、reduce()、filter().一.splice()
行列の内容は、既存の要素を削除したり、新しい要素を追加したりして変更されます.
var myFish = ["angel", "clown", "mandarin", "surgeon"];
// 2 0 , "drum"
var removed = myFish.splice(2, 0, "drum");
// myFish:["angel", "clown", "drum", "mandarin", "surgeon"]
// :[],
console.log("1:"+removed+"---"+myFish); //1:---angel,clown,drum,mandarin,surgeon
// 3 1
removed = myFish.splice(3, 1);
// myFish:["angel", "clown", "drum", "surgeon"]
// :["mandarin"]
console.log("2:"+removed+"---"+myFish); //2:mandarin---angel,clown,drum,surgeon
// 2 1 , "trumpet"
removed = myFish.splice(2, 1, "trumpet");
// myFish: ["angel", "clown", "trumpet", "surgeon"]
// :["drum"]
console.log("3:"+removed+"---"+myFish);//3:drum---angel,clown,trumpet,surgeon
// 0 2 , "parrot", "anemone" "blue"
removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
// myFish:["parrot", "anemone", "blue", "trumpet", "surgeon"]
// :["angel", "clown"]
console.log("4:"+removed+"---"+myFish);//4:angel,clown---parrot,anemone,blue,trumpet,surgeon
// 3 2
removed = myFish.splice(3, Number.MAX_VALUE);
// myFish: ["parrot", "anemone", "blue"]
// :["trumpet", "surgeon"]
console.log("5:"+removed+"---"+myFish);//5:trumpet,surgeon---parrot,anemone,blue
二.ピューシー()配列の末尾に1つ以上のパラメータを追加します.
var arr = [1,2,3];
arr.push(4); // arr [1,2,3,4]
三.ポップ()「抛り出す」配列の末尾の値は、例えば、
var arr=[1, 4, 6]
var oneDown = arr.pop();// oneDown 6 ,arr [1, 4]
四.shift()「シフト」の1つの配列の最初の値は、例えば、
var arr=[1, 4, 6]
var oneDown = arr.shift();// oneDown 1 ,arr [4, 6]
五.unshift()ヘッダに要素を追加します.例えば、
var arr = [1,2,3];
arr.unshift(4); // arr [4,1,2,3]
六.ハスOwnProperty()オブジェクトに属性があるかどうかをチェックします.
var myObj = {
top: "hat",
bottom: "pants"
};
myObj.hasOwnProperty("top"); // true
myObj.hasOwnProperty("middle"); // false
七.match()マッチする文字は、例えば以下のように使います.
var testString = "Hello World! How are you!Haha"; //
//
/* /
h
/
g global( ), 。
i , 。
*/
var expressionToGetSoftware = /h/gi;
var softwareCount = testString.match(expressionToGetSoftware).length;//softwareCount 4
八.map()反復配列は、例えば、
var oldArray = [1,2,3,4,5];
var newArray = oldArray.map(function(val){
return val+3;
});
console.log(newArray); //[4, 5, 6, 7, 8]
九.レデュース()行列を反復して、たとえば値に蓄積します.
var array = [4,5,6,7,8];
var singleVal = 0;
//
singleVal = array.reduce(function(qq,ww){
console.log("qq:"+qq+",ww:"+ww); //qq:4,ww:5 qq:9,ww:6 qq:15,ww:7 qq:22,ww:8
return qq+ww;
});
console.log(singleVal); //30
十.filter()1つの配列を繰り返し、与えられた条件で該当する要素をフィルタリングします.例えば、
var oldArray = [1,2,3,4,5,6,7,8,9,10];
var newArray = oldArray.filter (function(val){
return val<6; // 6
});
console.log(newArray); //[1, 2, 3, 4, 5]
// 。 JavaScript , false、null、0、""、undefined NaN。
// filter
function bouncer(arr) {
var newarr=arr.filter(function(val){
return !!val;
});
return newarr;
}
// ,
function bouncer(arr) {
return arr.filter(Boolean);
}
console.log(bouncer([7, "ate", "", false, 9])); //[7, "ate", 9]
console.log(bouncer([false, null, 0, NaN, undefined, ""])); //[]
console.log(bouncer([1, null, NaN, 2, undefined])); // [1, 2]