Created
November 19, 2012 14:54
-
-
Save YusukeHirao/4111091 to your computer and use it in GitHub Desktop.
配列を操作するjQueryのユーティリティ関数まとめ ref: http://qiita.com/items/8d1089c2bf86e7dc05ef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ECMA5から使えるforEachメソッドとはvalueとindexの引数の順番が逆 | |
[1, 2, 3].forEach(function (value, index, context) { | |
// loop | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var array = [1, 2, 3], | |
length = array.length; | |
(function () { | |
var i; | |
for (i = 0; i < length; i += i) | |
if (i === 2) { | |
return; // 無名関数まで抜けることができる。 | |
// break; ループだけ抜ける場合はコレ。 | |
} | |
}); | |
}()); | |
(function () { | |
$.each([1, 2, 3], function (i, value) { | |
if (i === 2) { | |
return; // $.eachまでしか抜けられない。 | |
} | |
}); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ECMA5から使えるmapメソッドと引数の順番は同じ | |
var newArray = [1, 2, 3].map(function (value, index, context) { | |
// return newValue; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 基本的にはjQueryの要素コレクションが返されるが、コールバック関数の戻り値に、jQuery要素もしくはDOM要素以外を | |
var result = $('div').map(function (index, element) { | |
// this === element は true | |
return element.nodeName; // 文字列を返してみる。 | |
}); | |
result; // インデックスには文字列が入っいるDOMを参照しない、できそこないのキメラのような中途半端なjQuery要素が出来上がる。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment