#foreachloop
Explore tagged Tumblr posts
Text
ð Unraveling the Magic of C#'s foreach Loop! ðâš
Ever felt lost in the complexity of looping through elements in C#? ð€¯ Fear not! Our latest blog post, "Understanding the foreach Loop," breaks it down into bite-sized, easy-to-grasp pieces. ð§ ð¡
ð Dive into the inner workings of the foreach loop, demystified with straightforward explanations and crystal-clear code examples. ð¥ïžðšâð»
ð Why Should You Care?
- Boost your code readability ð
- Wave goodbye to off-by-one errors ð«ð°ïž
- Write cleaner, bug-resistant code ðð
Ready to simplify your coding journey?
ð Click here ð [Unlock the Secrets of foreach](Why Does Collections In C# Does Not Implement IEnumerator? | Internals Of 'foreach' Loop) and level up your C# game! ðð
#CodingMadeEasy #ForEachLoop #CSharpMagic âš
0 notes
Link
C# Foreach Loop with Examples
0 notes
Photo

PHP Loop: For, While, Do While Foreach https://www.troposal.com/php-loop/ #Troposal #PHP #ForLoop #WhileLoop #DoWhileLoop #ForeachLoop #WebDevelopment #Website https://www.instagram.com/p/B_SBSpvnwaM/?igshid=1km6hbesmdkui
0 notes
Photo

Today, we are showing off how #foreachloop works! Important for iterating through lists, arrays, and other collections! What other #programming knowledge can we teach you? Let us know! https://www.instagram.com/keysmashstudios/p/BwYPuFqlTk8/?utm_source=ig_tumblr_share&igshid=od2k8qvqrdt9
0 notes
Text
Controls Experimentation
Character Possession
We looked into spawning new characters and possessing them. This can be used to spawn any type of actor such as items or enemies but weâre using a character with a skeletal mesh and so weâre able to take control of them.
I placed a box trigger (made visible for testing purposes) and created an BeginOverlap event for it.Â
I then was able use the SpawnActor node and select the character from the drop down list. The âGetActorLocationâ finds the target points location in the viewport and sets that as the spawn point for this new character. You can also use either a delay or a retriggerable delay so the DoOnce resets. This is in place so the player canât continuously spam new characters and flood the level with the old ones. If you for some reason want the player to be able to do this you can simply remove the DoOnce. The delay can also be placed at different parts of the code for example you can have the player needing to stand in the trigger for a certain amount of time before the possession or have it instant like I am.

Here you can see the old character is still running in place at the box trigger. This will remain here forever until told otherwise. For whatever purposes in your game you might want the character to do a different animation such as idle if you want to repossess this character later on. If however youâre finished with the old character and no longer need it is better to destroy it.
Collisions
Hereâs my blueprint for destroying the old actor. I simply added the destroy actor node right a the moment of the overlap. This is a much simpler way of doing it in my opinion than trying to make every actor overlapping the box trigger to get destroyed. That method could also cause issues as later in the game you might hit the trigger and get deleted by accident.
I also added a DoOnce so this happens once and never again. I also set the DestoryActorâs target as âGet Player Characterâ then added âcast to actorâ so only the player character activates the trigger box. This is because I only want the player character to be destroyed not every actor that might accidentally trigger the box.

You can also change how box triggers (and other triggers) work. There are different collision presets which can be used for different purposes. For example âOverlapAllDynamicâ will only trigger an overlap when dynamic actors hit the trigger such as projectiles or other moving actors.
Static actors wont trigger the overlap and so theyâre safe to use inside the trigger space. Dynamic collision triggers are taxing on the game however as theyâre constantly keeping track of all these dynamic actors.
Different collision presets might be needed for different moments in your game. The majority of issues seen in video games are collision problems so when building a game you always need to be aware of what collision youâre working on. Depending on any in game sequences, boss fights or character power ups/attacks might require different collision presets to work properly. Being aware of youâre collision and how it relates to your game will help you loads when coming across issues with your actors. Itâs easy to see how complex your blueprint can get especially when needing to change collision presets or actor tags in game.Â
Another collision test I did was seeing if a true/false branch could identify if the correct type of actor had entered the trigger. I was having issues as my character was not already placed in the game and instead spawned at the player network. I tried referencing back to âMyNewCharacterâ but it was not working so instead this new blueprint which makes use of the âGet all Actors of Classâ and âForEachLoopâ has fixed my issue!
Adding Controls
By going into project settings Iâm able to add action mappings such as controls for the character. I began experimenting with a Dash. I made it so the character has a short boost of speed. I also made it so a ghost is left behind of the character and vanished after a short while.Â


Once Iâve narrowed down exactly what kind of attacks and controls I want Iâll begin using the action mappings to place my control layout ingame. Iâll also begin looking into tutorials for creating additional movement such as double jumps, wall climbing and crouch!
0 notes
Text
JavaScriptã®Arrayé¢ä¿ãããã
ããã°ã©ã é¢ä¿ã§å¿
ããšèšã£ãŠäœ¿ããã®ãšèšãã°é
åïŒArrayïŒã§ããã JavaScriptã®é
åã¯ããããé¢çœãïŒããããããšãèšãïŒåäœãããã®ã§æ¹ããŠèªåçšã®ã¡ã¢ãšããŠããŸãšããŠãããŸãã
Array.length
JavaScriptã®é
åã®é·ãïŒèŠçŽ æ°ïŒãç¥ããããšãã«äœ¿ãã®ã¯ã¿ãªããããªãã¿ Array.length ã§ããããã® length ã®æåã«ã€ããŠæå€ãšç¥ããªã人ãããã®ã§æ¹ããŠããããããŠã¿ãŸãã
Array.length ã¯æ£ç¢ºã«ã¯èŠçŽ æ°ã§ã¯ãªãïŒ
ãããªã®ãŒïŒïŒ
以äžã®ãµã³ãã«ãèŠãŠãããããšã©ãããäºã ãçè§£ã§ãããããããŸããã
JavaScript
var words = ['ãã£ããŒãïŒ', 'ããŒã®ããŒïŒ']; // åï¿œï¿œï¿œç¶æ
console.log(words.length); // 2 // 代å
¥ãã words[2] = 'ãã¹ãªãããŒïŒ'; console.log(words.length); // 3 // Array.prototype.push ã䜿ã words.push('å€è¡æ§ã ãããã£ïŒ'); console.log(words.length); // 4 console.log(words); // ["ãã£ããŒãïŒ", "ããŒã®ããŒïŒ", "ãã¹ãªãããŒïŒ", "å€è¡æ§ã ãããã£ïŒ"] // 倧ããªã€ã³ããã¯ã¹ã«ä»£å
¥ããŠã¿ã words[100] = '倧äžå€«ãå€è¡æ§ã ããã£ïŒ'; // length 㯠5 ã§ã¯ãªã console.log(words.length); // 101 // é
åãã©ããªã£ãŠããã®ãèŠãŠã¿ã console.dir(words); /* Array[101] 0: "ãã£ããŒãïŒ" 1: "ããŒã®ããŒïŒ" 2: "ãã¹ãªãããŒïŒ" 3: "å€è¡æ§ã ãããã£ïŒ" 100: "倧äžå€«ãå€è¡æ§ã ããã£ïŒ" length: 101 */
words[4] ã words[99] ããªãã®ã« words.length 㯠101 ã«ãªã£ãŠãŸãã ãã®çµæãããããããã«ãæå³çã«æäœããªããã° Array.length ã¯æå€§ã®ã€ã³ããã¯ã¹ã®æ°ã«ãã©ã¹1ããæ°å€ã§ããããèŠçŽ ã101åããé
åããšããŠèªèãããŠããŸãã ãããŠãwords[4] ã words[99] ã¯å®è³ª undefined ãå
¥ã£ãŠããã®ãšïŒã»ãŒïŒåããšããè§£éãã§ããŸããïŒãªããã»ãŒããªã®ãã«ã€ããŠã¯åŸã»ã©è§ŠããŸãïŒ
Array.length ã®å€ã®å€æŽ
ãåç¥ã®ããã«ãArray.length ã¯èªã¿åãå°çšããããã£ã§ã¯ãªãå€ã倿Žããããšãã§ããŸãã 倿Žããæã®åäœã¯ã©ããªã£ãŠããã®ã§ããããã
JavaScript
var words = ['ãã£ããŒãïŒ', 'ããŒã®ããŒïŒ', 'ãã¹ãªãããŒïŒ']; // åæç¶æ
console.log(words.length); // 3 // çŸåšã®é·ããããå°ããå€ã«å€æŽãã words.length = 2; console.log(words); // ["ãã£ããŒãïŒ", "ããŒã®ããŒïŒ"] // çŸåšã®é·ãããã倧ããå€ã«å€æŽãã words.length = 10; console.log(words); // ["ãã£ããŒãïŒ", "ããŒã®ããŒïŒ"] // pushããŠã¿ã words.push('å€è¡æ§ã ãããã£ïŒ'); console.log(words.length); // 11 console.log(words); /* Array[101] 0: "ãã£ããŒãïŒ" 1: "ããŒã®ããŒïŒ" 10: "ãã¹ãªãããŒïŒ" length: 11 */
words.length = 2; ã«ããæã¯äºæ³éãã®åäœã«ãªããŸããããwords.length = 10; ã«ããŠãèŠãç®çã«ã¯å€ãããŸããã ããã length ã®å€ã¯éãã®ã§ããã®é
åã¯ãèŠçŽ ã10åããé
åããšããæ±ãã«ãªã£ãŠããŸãã ãªã®ã§ãpush ããæã«ã€ã³ããã¯ã¹ã 10 ããå§ãŸãããã§ãã
åäœãæš¡å£ããŠã¿ã
ããèãããšäžæè°ãªåäœããã Array.length ã§ãããã»ãŒåãåäœãæš¡å£ããŠã¿ããšãããªæãã§ãããããïŒèªä¿¡ãªãïŒ
JavaScript
// ä»ã¯ããããŸã䜿ãããªã new Array() ã¿ããã«äœ¿ãã function fakeArray() { // new ã€ãå¿ãããé§ç® if (!(this instanceof fakeArray)) { throw new TypeError('Constructor cannot be called as a function'); } var i = 0; // åŒæ°ããããªãå
¥ããŠãã for (; i < arguments.length; i++) { this[i] = arguments[i]; } // length ãå®çŸ©ãã Object.defineProperty(this, 'length', (function () { // å
éšã§æã€ length å€ var lengthTemp = i; return { // ã²ãã¿ãŒã®å®çŸ© get: function () { // çŸåšã® length ãã倧ããå Žå if (this.hasOwnProperty(lengthTemp)) { var maxIndex = lengthTemp - 1; // æå€§ã®ã€ã³ããã¯ã¹ãååŸãã for (var index in this) { if (this.hasOwnProperty(index) && maxIndex < parseInt(index, 10)) { maxIndex = parseInt(index, 10); } } lengthTemp = maxIndex + 1; } return lengthTemp; }, // ã»ãã¿ãŒã®å®çŸ© set: function (length) { length = parseInt(length, 10); if (isNaN(length)) { throw new RangeError('Invalid value'); } // çŸåšã® length ãããå°ããå€ã®å ŽåãäœèšãªèŠçŽ ãåé€ãã if (length < lengthTemp) { for (var index in this) { if (this.hasOwnProperty(index) && length - 1 < parseInt(index, 10)) { delete this[index]; } } } lengthTemp = length; }, }; })()); } fakeArray.prototype = { constructor: fakeArray, // é
åã£ãœãæ¯ãèãããã®æäœ push: Array.prototype.push, sort: Array.prototype.sort, splice: Array.prototype.splice, }; // é
åãã©ãã®çæ var words = new fakeArray('ãã£ããŒãïŒ', 'ããŒã®ããŒïŒ'); // åæç¶æ
console.log(words.length); // 2 // 代å
¥ãã words[2] = 'ãã¹ãªãããŒïŒ'; console.log(words.length); // 3 // push ã䜿ã words.push('å€è¡æ§ã ãããã£ïŒ'); console.log(words.length); // 4 console.log(words); // ["ãã£ããŒãïŒ", "ããŒã®ããŒïŒ", "ãã¹ãªãããŒïŒ", "å€è¡æ§ã ãããã£ïŒ"] // çŸåšã®é·ããããå°ããå€ã«å€æŽãã words.length = 2; console.log(words); // ["ãã£ããŒãïŒ", "ããŒã®ããŒïŒ"] // çŸåšã®é·ãããã倧ããå€ã«å€æŽãã words.length = 10; console.log(words); // ["ãã£ããŒãïŒ", "ããŒã®ããŒïŒ"] // ããäžåºŠpushããŠã¿ã words.push('å€è¡æ§ã ãããã£ïŒ'); console.log(words.length); // 11 console.log(words); /* fakeArray[101] 0: "ãã£ããŒãïŒ" 1: "ããŒã®ããŒïŒ" 10: "ãã¹ãªãããŒïŒ" length: 11 */
ã²ãã¿ãŒãšã»ãã¿ãŒã䜿ãããšã§ã»ãŒé
åã£ãœãåããã§ããŸããã Array.length ã¯èŠãç®ã®å²ã«è€éïŒã§ããããªãããããããŒïŒ
Array.prototype.forEach ãš for ãš for...of
é
åãã«ãŒããããæ¹æ³ãšããã°ãæ®éã® for ã䜿ããŸãããArray.prototype.forEach ããããŸããã ãã㊠ECMAScript 2015 (ES6) ãã䜿ããã€ãã©ãã«ãªããžã§ã¯ãã®ã«ãŒãæ§æ for...of ãç»å ŽããŸããã
JavaScript
var words = ['ãã£ããŒãïŒ', 'ããŒã®ããŒïŒ', 'ãã¹ãªãããŒïŒ', 'å€è¡æ§ã ãããã£ïŒ'], length = words.length; /* æ®éã® for */ for (var i = 0; i < length; i++) { console.log(words[i]); } /* Array.prototype.forEach */ words.forEach(function (word) { console.log(word); }); /* for...of */ for (var word of words) { console.log(word); } /* å
šéšåãçµï¿œï¿œï¿œ */ // "ãã£ããŒãïŒ" // "ããŒã®ããŒïŒ" // "ãã¹ãªãããŒïŒ" // "å€è¡æ§ã ãããã£ïŒ"
ããããã®éã
æ®éã® for ã¯ããã åã«æ¡ä»¶ã false ã«ãªããŸã§ã«ãŒããè¡ãæ§æã§ããé
åãšã¯çŽæ¥é¢ä¿ãªãæ§æã§ããããããé
åã«å©çšããŠããèš³ã§ããã Array.prototype.forEach ã¯ãèŠãŠãããããã« Array.prototype ã®ã¡ãœãããªã®ã§ãå®å
šã«é
åå°çšã®ã¡ãœããã§ããäºãããããŸãã for...of ã¯ãé
åã ãã§ã¯ãªããã€ãã©ãã«ãªããžã§ã¯ãããšãããªããžã§ã¯ãã§ããã°äœ¿çšã§ããã«ãŒãæ§æã§ããã€ãã©ãã«ãªããžã§ã¯ãã«ã€ããŠã¯ããã§ã¯è©³ãã説æããŸããã
åŠççãªé¢ã§ãããšãfor ãš for...of ã¯ã«ãŒããæãåºã break ããããŸãããArray.prototype.forEach ã«ã¯ãããŸãããïŒé¢æ°å®è¡åãªã®ã§ïŒ
JavaScript
var words = ['ãã£ããŒãïŒ', 'ããŒã®ããŒïŒ', 'ãã¹ãªãããŒïŒ', 'å€è¡æ§ã ãããã£ïŒ'], breakWord = 'ãã¹ãªãããŒïŒ', length = words.length; /* æ®éã® for */ for (var i = 0; i < length; i++) { if (words[i] === breakWord) { break; } console.log(words[i]); } // "ãã£ããŒãïŒ" // "ããŒã®ããŒïŒ" /* Array.prototype.forEach */ words.forEach(function (word) { if (word === breakWord) { break; } console.log(word); }); // SyntaxError: Illegal break statement // äžå¿ ifæ ã§äŒŒããããªçµæã«ã§ãããã // ç¡é§ãªã«ãŒããŸã§å®è¡ãããŠããŸã var flag = true; words.forEach(function (word) { if (flag) { if (word !== breakWord) { console.log(word); } else { flag = false; } } }); // "ãã£ããŒãïŒ" // "ããŒã®ããŒïŒ" /* for...of */ for (var word of words) { if (word === breakWord) { break; } console.log(word); } // "ãã£ããŒãïŒ" // "ããŒã®ããŒïŒ"
jQuery ã® $.each() 㯠return false; ãšããäºã§ break ãšåãäºãã§ããŸãããArray.prototype.forEach ã¯åŠçã¯æ¢ãŸããŸããã
ä»ã«ããçŸåšåŠçäžã®æãäžæããŠæ¬¡ã®ã«ãŒãåŠçãè¡ã continue ããããŸããããã㯠Array.prototype.forEach ã§ã¯ return ã§ä»£çšã§ããŸãã
JavaScript
var words = ['ãã£ããŒãïŒ', 'ããŒã®ããŒïŒ', 'ãã¹ãªãããŒïŒ', 'å€è¡æ§ã ãããã£ïŒ'], ignoreWord = 'ãã¹ãªãããŒïŒ', length = words.length; /* æ®éã® for */ for (var i = 0; i < length; i++) { if (words[i] === ignoreWord) { continue; } console.log(words[i]); } // "ãã£ããŒãïŒ" // "ããŒã®ããŒïŒ" // "å€è¡æ§ã ãããã£ïŒ" /* Array.prototype.forEach */ words.forEach(function (word) { if (word === ignoreWord) { return; } console.log(word); }); // "ãã£ããŒãïŒ" // "ããŒã®ããŒïŒ" // "å€è¡æ§ã ãããã£ïŒ" /* for...of */ for (var word of words) { if (word === ignoreWord) { continue; } console.log(word); } // "ãã£ããŒãïŒ" // "ããŒã®ããŒïŒ" // "å€è¡æ§ã ãããã£ïŒ"
åŠçé床
ããŠãã©ããäžçªåŠçãæ©ãã®ï¿œï¿œãããã10000åã«ãŒããããŠèšæž¬ããŠã¿ãŸãã
JavaScript
// 10000åèŠçŽ ãããé
åãäœã var testArray = []; for (var i = 0; i < 10000; i++) { testArray[i] = i; } function forLoop() { var length = testArray.length, sum = 0; console.time('for'); for (var i = 0; i < length; i++) { sum += testArray[i]; } console.timeEnd('for'); } function forEachLoop() { var sum = 0; console.time('forEach'); testArray.forEach(function (value) { sum += value; }); console.timeEnd('forEach'); } function forOfLoop() { var sum = 0; console.time('for...of'); for (var value of testArray) { sum += value; } console.timeEnd('for...of'); }
forLoop() ãš forEachLoop() ãš forOfLoop() ããããã5åå®è¡ããŠã¿ãŸããã ïŒããŒãžã§ã³ãPCçã®ç°å¢ã«ãå·Šå³ãããã®ã§ãããŸã§åèå€ã§ãïŒ
Node.js (v7.5.0) ã§å®è¡
1åç® 2åç® 3åç® 4åç® 5åç® å¹³å forLoop() 0.029ms 0.029ms 0.026ms 0.033ms 0.027ms 0.029ms forEachLoop() 0.210ms 0.235ms 0.236ms 0.235ms 0.233ms 0.230ms forOfLoop() 0.331ms 0.367ms 0.282ms 0.368ms 0.328ms 0.335ms
Google Chrome 56 ã®ããããããŒããŒã«ã§å®è¡
1åç® 2åç® 3åç® 4åç® 5åç® å¹³å forLoop() 0.029ms 0.024ms 0.026ms 0.028ms 0.029ms 0.027ms forEachLoop() 0.198ms 0.224ms 0.198ms 0.198ms 0.233ms 0.210ms forOfLoop() 0.199ms 0.200ms 0.254ms 0.229ms 0.178ms 0.212ms
ããããæ®éã® for ã¯æ©ãã§ãããé
åé¢ä¿ãªãã«ã«ãŒããè¡ãã®ã匷ãã®ã§ããããã Node.js ã§ã¯äžçªé
ãã®ã¯ for...of ã§ãããChromeã§ã¯ Array.prototype.forEach ãš for...of ãã»ãŒåãã§ãã
Array.prototype.forEach ã¯ãæ¯å颿°ãå®è¡ããŠããäºãšãã€ãã£ãã³ãŒãå
ã§æ§ã
ãªå€å®ãè¡ãªã£ãŠããã®ã§å°ãåŠçã«æéããããããã§ãã MDNã® Array.prototype.forEach() - JavaScript | MDN ã®äºææ§ã®é
ç®ãèŠããšããããã£ãåŠçãè¡ãããŠããã®ãããããŸãããªãã»ã©ãŒïŒ
for...of ã¯ãäžèŠæ®éã® for æ§æã«äŒŒãŠããŸãããã€ãã©ãã«ãªããžã§ã¯ããå埩ããããã®é¢æ°å®è¡ãç°¡ç¥ããèšè¿°ã¿ãããªãã®ãªã®ã§ããããå
éšã§é¢æ°å®è¡ãè¡ãããŠããé¢ä¿ã§å°ãæéããããããã§ãã
é
åã§ã® undefined
çµæ§ãããããã®ããundefined ã§ãããã®æ±ãã¯çµæ§åä»ã§ããïŒé
åã ããããªããŠãªããžã§ã¯ãå
šäœã«èšãããã§ããã©ãïŒ
ãèŠçŽ ãæªå®çŸ©ããšãå€ãæªå®çŸ©ã
undefined ã¯ãã®åã®éããæªå®çŸ©ããšããæå³ã§ããã以äžã®ãããªå Žåã¯äœãéã£ãŠããã®ã§ãããã
JavaScript
/* ã€ã³ããã¯ã¹ãé£ã°ããŠé
åã«å
¥ããŠã¿ã */ var a = ['ããã£ããããïŒ']; a[2] = 'ãã£ããããŠãã£ãŠãïŒ'; console.log(a); /* Array[101] 0: "ããã£ããããïŒ" 2: "ãã£ããããŠãã£ãŠãïŒ" length: 3 */ // èŠçŽ ãæªå®çŸ©ãªã®ã§ undefined console.log(a[1]); // undefined // for ã§ã«ãŒãããŠã¿ããšãæªå®çŸ©é¢ä¿ãªãåŠçãã for (var i = 0; i < 3; i++) { console.log(a[i]); } // "ããã£ããããïŒ" // undefined // "ãã£ããããŠãã£ãŠãïŒ" // for...of ã§ã«ãŒãããŠã¿ããšãfor ãšåãçµæ for (var word of a) { console.log(word); } // "ããã£ããããïŒ" // undefined // "ãã£ããããŠãã£ãŠãïŒ" // Array.prototype.forEach ã§ã«ãŒãããŠã¿ããšãæªå®çŸ©èŠçŽ ã¯ç¡èŠããã a.forEach(function (word) { console.log(word); }); // "ããã£ããããïŒ" // "ãã£ããããŠãã£ãŠãïŒ" /* ããããã undefined ãé
åã«å
¥ããŠã¿ã */ var b = ['ããã£ããããïŒ', undefined, 'ãã£ããããŠãã£ãŠãïŒ']; console.log(b); /* Array[101] 0: "ããã£ããããïŒ" 1: undefined 2: "ãã£ããããŠãã£ãŠãïŒ" length: 3 */ // å€ã undefinedïŒæªå®çŸ©ïŒ console.log(b[1]); // undefined // ãã¹ãŠåãçµæ for (var i = 0; i < 3; i++) { console.log(b[i]); } for (var word of b) { console.log(word); } b.forEach(function (word) { console.log(word); }); // "ããã£ããããïŒ" // undefined // "ãã£ããããŠãã£ãŠãïŒ" // ᅵᅵã€ã®å€ã¯çããã®ãïŒ console.log(a[1] === b[1]); // true // Array.prototype.indexOf ã§èª¿ã¹ãŠã¿ããšã©ããªã®ã console.log(a.indexOf(undefined)); // -1 console.log(b.indexOf(undefined)); // 1 // inæŒç®åã§èª¿ã¹ãŠã¿ããšã©ããªã®ã console.log(1 in a); // false console.log(1 in b); // true // hasOwnProperty() ã§èª¿ã¹ãŠã¿ããšã©ããªã®ã console.log(a.hasOwnProperty(1)); // false console.log(b.hasOwnProperty(1)); // true
ãããªäºãããŒãïŒãšããããã³ãã¯ããã£ãšãã§ãã
Array.prototype.forEach ã§åäœãéããŸãããããŠãArray.prototype.indexOf ã inæŒç®åãhasOwnProperty() ã§çµæãç°ãªã£ãŠããŸãã ããããa[1] === b[1] 㯠true ã§ããããã¯ã©ãããäºãªã®ãâŠâŠ
JavaScriptã§ã¯ããªããžã§ã¯ãã¯ååšããªãããããã£ïŒããã§ã¯é
åãªã®ã§ã€ã³ããã¯ã¹ïŒãæå®ããå Žåããšã©ãŒãšã¯ããã« undefined ãè¿ãããã«ãªã£ãŠããŸãã ãªã®ã§ããèŠçŽ èªäœããªãã®ã§ undefinedããšãèŠçŽ ã®å€ã undefinedããæ··åããŠããŸã£ãŠããã®ã§ãã é
åã§ Array.prototype.forEach ã Array.prototype.indexOf ã䜿çšããæã¯æ°ãä»ããªããšçãç®ã«åããããããŸããã
åºå¥ããæ¹æ³
Array.prototype.indexOf ã inæŒç®åãhasOwnProperty() ã§çµæãéãã®ã§ãããããæ¹æ³ã¯ãããŸãã ãªããžã§ã¯ãã«ã¯å¿
ã hasOwnProperty() ãçšæãããŠããŸãã ãã®ã¡ãœããã¯ããã®ããããã£ãæã£ãŠãããã®ççå€ïŒtrue / falseïŒãè¿ããŸãããŸãã«ãããè¯ãããã§ãã
JavaScript
var a = ['ããã£ããããïŒ']; a[2] = 'ãã£ããããŠãã£ãŠãïŒ'; var b = ['ããã£ããããïŒ', undefined, 'ãã£ããããŠãã£ãŠãïŒ']; console.log(a[1]); // undefined console.log(b[1]); // undefined console.log(a.hasOwnProperty(1)); // false console.log(b.hasOwnProperty(1)); // true
Array.prototype.push.apply ãš concat
é
åå士ãçµåãããæ¹æ³ãšã㊠Array.prototype.push.apply ãš Array.prototype.concat ããããŸãã åãããã«èŠããŠéããã®äž¡è
ãåŠçé床ãå€ãã£ãŠããããã§ãã
ããããã®éã
push ã¯ãç Žå£çãªçµåãã§ãconcat ã¯ãéç Žå£çãªçµåããšèšããŸãã
JavaScript
var words = ['ãžã£ããªã«ãã§ãžãïŒ', 'ãã£ããããŠãã£ãŠãïŒ']; /* Array.prototype.push.apply */ var words01 = ['ããã£ããããïŒ', 'ãããããâ']; var result01 = Array.prototype.push.apply(words01, words); // words01 èªäœãå€ãã console.log(words01); // ["ããã£ããããïŒ", "ãããããâ", "ãžã£ããªã«ãã§ãžãïŒ", "ãã£ããããŠãã£ãŠãïŒ"] // ã¡ãªã¿ã« push ã®è¿ãå€ã§ãã result01 ã¯çµååŸã®é
åã®é·ã console.log(result02); // 4 /* concat */ var words02 = ['ããã£ããããïŒ', 'ãããããâ']; var result02 = words02.concat(words); // words02 ã¯å€åããªã console.log(words02); // ["ããã£ããããïŒ", "ãããããâ"] // result02 ã«çµååŸã®é
åãå
¥ã console.log(result02); // ["ããã£ããããïŒ", "ãããããâ", "ãžã£ããªã«ãã§ãžãïŒ", "ãã£ããããŠãã£ãŠãïŒ"]
Array.prototype.push.apply ã¯å
ãšãªãé
åã®åŸãã«é
åãçµåããArray.prototype.concat ã¯äºã€ã®é
åãçµåããæ°ããé
åãè¿ãåŠçãšããéãããããŸãã
åŠçé床
concat ã¯æ°ããé
åãçæããŠãããšããç¹ãèãããšåŠççã«é
ãããªã€ã¡ãŒãžããããŸããå®éã«ã¯ã©ãã§ãããã 10000åçµåããåŠçã®é床ãç°¡åã«èšæž¬ããŠã¿ãŸãã
JavaScript
function pushTest() { var a = ['ããã£ããããïŒ', 'ãããããâãžã£ããªã«ãã§ãžãïŒ'], b = [ 'ã©ããã©ããïŒ', 'ãã£ããããŠãã£ãŠãïŒ', 'ãããŸãã£âãŠãããïŒ', 'ãã£ãšã客ãããæ¥ãŠãããããïŒ', ], push = Array.prototype.push; console.time('push test'); for (var i = 0; i < 10000; i++) { push.apply(a, b); } console.timeEnd('push test'); } function concatTest() { var a = ['ããã£ããããïŒ', 'ãããããâãžã£ããªã«ãã§ãžãïŒ'], b = [ 'ã©ããã©ããïŒ', 'ãã£ããããŠãã£ãŠãïŒ', 'ãããŸãã£âãŠãããïŒ', 'ãã£ãšã客ãããæ¥ãŠãããããïŒ', ]; console.time('concat test'); for (var i = 0; i < 10000; i++) { a = a.concat(b); } console.timeEnd('concat test'); }
pushTest() ãš concatTest() ããããã5åå®è¡ããŠã¿ãŸããã ïŒããŒãžã§ã³ãPCçã®ç°å¢ã«ãå·Šå³ãããã®ã§ãããŸã§åèå€ã§ãïŒ
Node.js (v7.5.0) ã§å®è¡
1åç® 2åç® 3åç® 4åç® 5åç® å¹³å pushTest() 0.685ms 0.613ms 0.696ms 0.605ms 0.753ms 0.670ms concatTest() 397.911ms 397.856ms 400.857ms 398.586ms 407.725ms 400.587ms
Google Chrome 56 ã®ããããããŒããŒã«ã§å®è¡
1åç® 2åç® 3åç® 4åç® 5åç® å¹³å pushTest() 0.484ms 0.463ms 0.572ms 0.575ms 0.501ms 0.519ms concatTest() 958.716ms 957.499ms 965.490ms 954.975ms 962.546ms 959.845ms
ãããå·®ãåºãŠããŸããŸãããconcat 㯠Node.js ã§ã¯ãŸã ãã·ã¬ãã«ã§ããããChromeã§ã¯ã»ãŒ1ç§ããã£ãŠãŸãã ããŸãçŸå®çã§ã¯ãªãæž¬å®æ¹æ³ãªã®ã§ãä»åºŠã¯äžåºŠã ãçµåãè¡ãåŠçã10000åå®è¡ããããã«ããŠã¿ãŸãã
JavaScript
function pushTest() { var push = Array.prototype.push, a, b; console.time('push test'); for (var i = 0; i < 10000; i++) { a = ['ããã£ããããïŒ', 'ãããããâãžã£ããªã«ãã§ãžãïŒ']; b = [ 'ã©ããã©ããïŒ', 'ãã£ããããŠãã£ãŠãïŒ', 'ãããŸãã£âãŠãããïŒ', 'ãã£ãšã客ãããæ¥ãŠãããããïŒ', ]; push.apply(a, b); } console.timeEnd('push test'); } function concatTest() { var a, b; console.time('concat test'); for (var i = 0; i < 10000; i++) { a = ['ããã£ããããïŒ', 'ãããããâãžã£ããªã«ãã§ãžãïŒ']; b = [ 'ã©ããã©ããïŒ', 'ãã£ããããŠãã£ãŠãïŒ', 'ãããŸãã£âãŠãããïŒ', 'ãã£ãšã客ãããæ¥ãŠãããããïŒ', ]; a.concat(b); } console.timeEnd('concat test'); }
ãã® pushTest() ãš concatTest() ããããã5åå®è¡ããŠã¿ãŸããã ïŒããŒãžã§ã³ãPCçã®ç°å¢ã«ãå·Šå³ãããã®ã§ãããŸã§åèå€ã§ãïŒ
Node.js (v7.5.0) ã§å®è¡
1åç® 2åç® 3åç® 4åç® 5åç® å¹³å pushTest() 0.781ms 0.829ms 0.775ms 0.814ms 0.937ms 0.827ms concatTest() 3.290ms 2.614ms 2.882ms 2.060ms 2.627ms 2.695ms
Google Chrome 56 ã®ããããããŒããŒã«ã§å®è¡
1åç® 2åç® 3åç® 4åç® 5åç® å¹³å pushTest() 1.655ms 1.858ms 1.562ms 1.386ms 1.612ms 1.615ms concatTest() 2.625ms 2.734ms 2.339ms 2.365ms 2.597ms 2.532ms
Node.js ã¯çŽ2msã®å·®ã§ãChromeã§ã¯çŽ1msã®å·®ã§ããã ããã»ã©æ°ãå€ããªãé
åãäžåºŠã ãçµåããã®ã§ããã°ããŸãå€ãããªãããã§ãããç¹ã« concat ããçç±ããªããã° push ã§ãããããããŸããã
0 notes
Photo

âswoleâ #houdini #sidefx #aftereffects #goprocedural #procedural #create #make #vfx #design #3d #creative #3dart #abstract #visuals #motiongraphics #everyday #dailyrender #mantra #loop #forloop #foreachloop #swole #swollen #macro #swell #dof #microscopic
#swollen#design#abstract#goprocedural#procedural#visuals#swole#dailyrender#microscopic#sidefx#aftereffects#swell#creative#make#vfx#dof#everyday#3d#create#loop#forloop#3dart#foreachloop#houdini#motiongraphics#mantra#macro
2 notes
·
View notes
Photo

âvertigoâ #houdini #sidefx #aftereffects #goprocedural #procedural #create #make #vfx #design #3d #creative #3dart #abstract #visuals #motiongraphics #everyday #dailyrender #mantra #loop #forloop #foreachloop #cubes #cubeart
#vfx#mantra#cubes#abstract#creative#make#goprocedural#create#houdini#design#loop#3d#procedural#3dart#dailyrender#sidefx#forloop#motiongraphics#visuals#aftereffects#cubeart#everyday#foreachloop
1 note
·
View note