çåã¡ã¢: JavaScriptã§é åããªãã¸ã§ã¯ãã®ãã¼ãå復ããã¤ãã£ãªã
ãJavaScriptã§ã¯ãã¤ãã£ãã®foræã使ããªãæ¹ããããï¼ãã¨æãã¦ãããã¾ããã
ãã¤ãã£ãã®forã®ä»£ããã«ãArray.forEachããUnderscore.jsã«ãããã³ãªãªé¢æ°ã使ãããã¨ã®ãã¨ã§ãããããããã°ã¹ã³ã¼ããéå®ã§ãããããã¨ããã®ãçç±ã ã£ãã¨æãã¾ãã
çµè¾¼ã¿ã®ç¹°ãè¿ãæ§æã®ä½¿ç¨ãéæ¨å¥¨ã®è¨èªãªãã¦ãåãã¦èããã®ã§ã³ã£ããã§ãã
ã¨ããããã§ãfor inãArrays.forEachã試ãã¦ããã¨ãããã¢ã¤ã¢ã¤ã¨åãããªããã¨ãããã®ã§ã¡ã¢ãã¾ãã
ï¼è¿½è¨ï¼è§£æ±ºç·¨ãæ¸ãã¾ãããhttp://d.hatena.ne.jp/torazuka/20130304/for
é åã®å復ã«ã¯ãfor inãArray.forEachã使ããï¼ã®ï¼
for in
var foo = {}; var hoge = [1, 3, 5]; for(var n in hoge){ foo[n] = hoge[n]; } console.log(foo);
Javaã®æè¦ã ã¨ãnã«ã¯é åã®åè¦ç´ ãå ¥ã£ã¦ãã¦æ¬²ããã§ãããå®éã«ã¯é åã®indexãå ¥ãã¾ãã
çµæã¯ãããªãã¾ãã
{ '0': 1, '1': 3, '2': 5 }
ãããããã¨ã¯ã§ãããã©ããããã使ãæ¹ããããã®ãªã®ãåããã¾ããã
é åãå復ãããã¨ãã¯ããµã¤ãã®forã使ãæ°ããã¾ããã©ããªãã§ãããï¼
Array.forEach
var foo = {}; var hoge = [1, 3, 5]; var fill = function(element, index, array){ foo[index] = element; }; hoge.forEach(fill); console.log(foo);
forEachã®å¼æ°ã«ãåè¦ç´ ãå¦çããé¢æ°ã渡ãã¾ãããã®é¢æ°ï¼ä¸ã®ã³ã¼ãã ã¨fillã¨åã¥ãããã®ï¼ã®ã·ã°ããã£ããè¦æ ¼ã§æ±ºãããã¦ãããã§ããã
çµæã¯ãfor inã¨åãã«ãªãã¾ãã
{ '0': 1, '1': 3, '2': 5 }
ãªãã¸ã§ã¯ãã®ãã¼ã®å復ã«ã¯ãfor inã¯ä½¿ãããArray.forEachã¯ä½¿ããªã
for inã使ã£ã¦ããããªãã¸ã§ã¯ãã®ããããã£ããå¥ã®ãªãã¸ã§ã¯ãã«ã³ãã¼ããå¦çã«ã¤ãã¦ãèãã¾ãã
# ã¾ããã¦ãJavaScript The Good Partsã®åçµã§ãã
for in
var assert = require('assert'); var _ = require("../lib/underscore-min.js"); describe('Object', function(){ it('3.7. should return properties', function(){ var stooge = { "first-name" : "Jarome", "last-name" : "Howard" }; if(typeof Object.create !== 'function') { Object.create = function (o) { var F = function () {}; F.prototype = o; return new F(); }; } // stoogeã®ããããã£ãç¶æ¿ããanother_stoogeãä½ã£ã¦ãããã¤ãããããã£ã追å var another_stooge = Object.create(stooge); another_stooge['first-name'] = "Harry"; another_stooge['middle-name'] = "Moses"; another_stooge.nickname = "Moe"; stooge.profession = "actor"; // another_stoogeã®function以å¤ã®ããããã£ãfooã«ã³ãã¼ãã var foo = {}; // need init var name; for (name in another_stooge) { if(typeof another_stooge[name] !== 'function'){ foo[name] = another_stooge[name]; } } assert(foo["first-name"] === "Harry"); // stoogeã®ããããã£ï¼another_stoogeã§ä¸æ¸ãæ¸ã¿ï¼ assert(foo["middle-name"] === "Moses"); // another_stoogeã®ãããã㣠assert(foo["nickname"] === "Moe"); // another_stoogeã®ãããã㣠assert(foo["last-name"] === "Howard"); // stoogeã®ãããã㣠assert(foo["profession"] === "actor"); // stoogeã«å¾ãã足ããããããã£ï¼another_stoogeããåç §å¯è½ï¼ }) });
ãã®for inããä½ãå¥ã®ãã®ã§ä»£ç¨ããæ¹ãããã®ãï¼ ããããã¨ããããä½ã使ãã°ããã®ãï¼ ã¨ããã®ãçåã
Arrays.forEach
æåã¯ãããªãµãã«æ¸ãããã®ã®ãï¼å½ç¶ï¼ã¨ã©ã¼ã«ãªãã¾ããã
var foo = {}; var fill = function(element, index, array){ foo[index] = element; }; another_stooge.forEach(fill);
TypeError: Object #<Object> has no method 'forEach'
- forEachã¯é åã®ããã®é¢æ°
- é åã¯ãªãã¸ã§ã¯ãã ãããªãã¸ã§ã¯ãã¯ããªããããé åã§ã¯ãªã
ã£ã¦ãã¨ã§ãããã
ã¤ãã§ã«ãé åã®å復ã¨ãªãã¸ã§ã¯ãã®ãã¼ã®å復ãæ··åãã¦ãããã¨ã«ãJavaScriptãã¯ããã¯ãã¤ãã«ã¨ããæ¬ã®forEachã®é ç®ãèªãã§æ°ã¥ãã¾ãã(^^; ç´ æ´ãããæ¬ã§ããã
for each in
Wikipediaã®Foreachæï¼http://ja.wikipedia.org/wiki/Foreach%E6%96%87ï¼ã«è¼ã£ã¦ãããã©ãèªåã®ç°å¢ã§ã¯åããªãã£ãã®ã§çç¥ãå®è£ ããã¦ãå¦çç³»ãããã®ããªã
for each ( å¤æ° in ãªãã¸ã§ã¯ã ) {
æ
}
ï¼ãã ã®for inã¨ä½ãéããã ããï¼ï¼
Underscore.jsã®keys
Underscore.jsã«ã¯ããªãã¸ã§ã¯ãã®ãã¼ãæãåºãé¢æ°ããããããªã®ã§ã使ã£ã¦ã¿ã¾ããã
var foo = {}; var keys = _.keys(another_stooge); _.each(keys, function(name){ if(typeof another_stooge[name] !== 'function'){ foo[name] = another_stooge[name]; } }); assert(foo["first-name"] === "Harry"); assert(foo["middle-name"] === "Moses"); assert(foo["nickname"] === "Moe"); assert(foo["last-name"] === "Howard"); // failed! assert(foo["profession"] === "actor"); // failed!
ãã£ã¨ã_.keysã¯ããã¤ãã£ãã®for inã¨éã£ã¦ããããã¿ã¤ããã§ã¼ã³ãããã®ã¼ã£ã¦ã¯è¦ã¦ãããªããã§ããã
stoogeã«å ããããããããã£ã ãããã³ãã¼ããã¾ããã
çåã¾ã¨ã
- for inã¯é åã®å復ã«ä½¿ã£ã¦ãããã®ï¼
- ãªãã¸ã§ã¯ãã®ãã¼ã®å復ã«ä½¿ã第ä¸åè£ã¯ãfor inã§ããï¼ éãå ´åãä½ã使ãã®ãå®ç³ï¼
åãã£ãã追è¨ãï¼è¿½è¨ï¼è§£æ±ºç·¨ãæ¸ãã¾ãããhttp://d.hatena.ne.jp/torazuka/20130304/for