Ajax prototype.js 配列の中のnull、undefined要素を削除する

-


Topページ  >  お勉強  >  Ajax  >  配列の中のnull、undefined要素を削除する 

配列の中のnull、undefined要素を削除する

配列オブジェクトの中で、値がnull、もしくはundefinedの要素が存在した場合、削除します。




prototype.js


使用バージョン:1.5.1 公式サイト





1. ダウンロードしたprototype.jsを読み込みます。

<script type="text/javascript"
    src="prototype.js"></script>


2. compactメソッドを実行します。

// 配列を宣言する
var beforeArray = new Array(4);
// 配列の要素に値をセットする…が2つの要素には値をセットしない
beforeArray[0] = "value01";
beforeArray[1] = null;
beforeArray[3] = "value04";

// 配列の値がnull、もしくはundefinedの要素を削除する
// これにより、4要素あった配列は2要素になる
var afterArray = beforeArray.compact();




resultArray = targetArray .compact( );


resultArray null、undefined要素が削除された結果の配列
targetArray 削除対象の配列



↓インラインフレーム内でサンプルが動作しています。






Topページ  >  お勉強  >  Ajax  >  配列の中のnull、undefined要素を削除する 






-