ホームページ  >  記事  >  ウェブフロントエンド  >  js オブジェクト JS 配列オブジェクト操作の概要_JavaScript スキル

js オブジェクト JS 配列オブジェクト操作の概要_JavaScript スキル

WBOY
WBOYオリジナル
2016-05-16 18:12:21850ブラウズ

しかし、スクリプトのprototype.jsを学習する過程で、私たちがめったに使用しないメソッドがいくつかあることに気づきましたが、いくつかのメソッドは非常に古典的であるように見えます。スクリプトライブラリは常に増加しており、目の前にあるものはすべて...今日は JS の紹介を続け、Array 配列について学びましょう。

まず、その定義を見てみましょう:

コードをコピーします コードは次のとおりです。

var arrayObj = new Array()
var arrayObj = new Array([サイズ])
var arrayObj = new Array([element0[, element1[, [, elementN]] ]])

ここで: arrayObj は、Array オブジェクトに割り当てられた変数名です。
size 配列の添字は 0 から始まるため、作成される要素の添字は 0 から size -1 までとなります。
element0,...,elementN これにより、n 1 個の要素を含む長さ n 1 の配列が作成されます。この構文を使用するには、複数の要素が必要です。

Array のコンストラクターに引数が 1 つだけ渡され、その引数が数値の場合、それは符号なし 32 ビット整数 (約 40 億) でなければなりません。この値が配列のサイズになります。値が数値であるが 0 未満であるか、整数ではない場合、実行時エラーが発生します。

Array コンストラクターに渡される値が数値ではなく単一の値である場合、length プロパティは 1 に設定され、一意の要素の値が単一の渡される引数になります。 。

JS 配列は解析された配列であるため、複数の要素を配列に割り当てることができますが、実際にはデータを含む要素のみが存在します。これにより、配列で使用されるメモリの量が削減されます。

Array オブジェクトには、コンストラクター、長さ、プロトタイプという 3 つの組み込みプロパティがあります。コンストラクター、プロトタイプ、引数、およびその他のオブジェクト プロパティについては別の記事で説明するので、ここでは詳しく説明しません。以下では、主に Array の組み込みメソッドのいくつかを見ていきます。これは私たちにとって非常に重要です。なぜなら、このメソッドは頻繁に使用するからです。

concat メソッド: 2 つ以上の配列を連結し、新しい配列を返します。新しい配列に接続されている配列からコピーされた (参照型) オブジェクト パラメーターは、コピー後も同じオブジェクトを指していることに注意してください。新しい配列とソース配列のどちらが変更されても、もう一方は変更されません。 Change; (値の型) 新しい配列に接続された数値または文字列の場合、一方の配列の値の変更は、もう一方の配列の値には影響しません。例:

コードをコピー コードは次のとおりです。

function ConcatArrayDemo(){
var a , b, c, d;
a = 新しい配列(1,2,3);
c = 新しい配列(42, "VBScript); >d = a.concat(b, c);
// 配列 [1, 2, 3, "JScript", 42, "VBScript"]
return(d);


結合メソッド
: 特定の区切り文字に従って配列内の項目を文字列に変換し、それを返します。デフォルトの区切り文字はカンマです。例:

コードをコピーします
コードは次のとおりです。function JoinDemo(){ var a, a; = new Array(0, 1,2,3,4);
b = a.join("-")
//Return: "0-1-2-3-4"
return(b);
}



pop メソッド:
配列の最後の要素を削除し、配列が空の場合はその要素を返します。
プッシュ メソッド:
は、出現する順序で新しい要素を追加します。
パラメーターの 1 つが配列の場合、その配列は単一の要素として配列に追加されます。複数の配列内の 2 つまたは要素を結合する場合は、concat
メソッドを使用してください。
reverse メソッド は、Array オブジェクト内の要素の位置を反転します。実行中、このメソッドは新しい Array

オブジェクトを作成しません。配列が連続していない場合、reverse メソッドは配列内に要素を作成して配列内のギャップを埋めます。この方法で作成されたすべての要素の値は未定義です。 コードをコピー コードは次のとおりです:


function ReverseDemo(){
var a, l; // Declare variables.
a = new Array(0,1,2,3,4); // Create an array and assign values.
l = a.reverse(); // Reverse the contents of the array.
//Return: l=[4,3,2,1,0]
return(l); // Return the result array.
}

shift method removes the first element in the array and returns that element.

slice method returns an Array object, which contains the specified part of arrayObj. The slice method copies up to, but not including, the element specified by end. If start is negative, treat it as length start, where length is the length of the array. If end is negative, treat it as length end, where length is the length of the array. If end is omitted, then the slice method will copy to the end of arrayObj. If end appears before start, no elements are copied to the new array. Example:
Copy code The code is as follows:

//Except for the last element, myArray All elements in are copied to newArray:
newArray = myArray.slice(0, -1)

The sort method sorts the Array objects appropriately; it does not A new Array object is created. If a function is provided for the sortfunction argument, the function must return one of the following values:

Negative value if the first argument passed is smaller than the second argument.
Zero if both arguments are equal.
Positive value if the first parameter is larger than the second parameter.
Copy code The code is as follows:

function SortDemo(){
var a, l ; // Declare variables.
a = new Array("X" ,"y" ,"d", "Z", "v","m","r");
l = a.sort(); // Sort array.
return(l); // Return the sorted array.
}

splice method can remove the specified number of elements starting from the start position and insert new elements , thus modifying arrayObj. The return value is a new Array object consisting of the removed elements. The format is as follows:
Copy code The code is as follows:

arrayObj.splice(start, deleteCount, [ item1[, item2[, . . . [,itemN]]]])

toLocaleString method will be explained in the Date object, generally this method It is just returned to the user and not calculated in the code. The

unshift method inserts these elements into the beginning of an array, so they will appear in the array in the order in which they are passed. The format is as follows:
Copy code The code is as follows:

arrayObj.unshift([item1[, item2 [, . . . [, itemN]]]])

valueOf method&toString() The elements of the array are converted to strings. These strings are separated by commas and concatenated. together. Its operation is the same as the Array.toString and Array.join methods.

At this point, everything about the Array object is almost complete. I sorted them out to consolidate my scripting knowledge. I would like to reiterate that many of the examples are left by predecessors. If they are useful to you Sorry for any discomfort!
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。