I have a base array of 6 different words that represent game elements. Just wondering how I would multiply the number of instances of each word within the array.
var gameItem = [cattle, wood, stone, metal, grain, craft];
That is, say I want 10 instances of each word to be added to the array, how would I do that? Order isn't important as it'll be shuffled.
-
6why not a loop?StarPinkER– StarPinkER2013年02月04日 01:23:04 +00:00Commented Feb 4, 2013 at 1:23
-
@JermaineXu ; I was hoping for something that isn't performance intensive.C_K– C_K2013年02月04日 01:31:22 +00:00Commented Feb 4, 2013 at 1:31
-
Worry about performance when you have a performance problem.Felix Kling– Felix Kling2013年02月04日 01:33:25 +00:00Commented Feb 4, 2013 at 1:33
-
@FelixKling you don't have performance problems when you 'worry about performance' by design ;-) But you sure have a good point here. That being said, it's not forbidden to use loops; even in performance critical situations. Sometimes it's just the right way.Robin van Baalen– Robin van Baalen2013年02月04日 01:36:57 +00:00Commented Feb 4, 2013 at 1:36
-
@RobinvanBaalen - I disagree. You don't have performance problems when you use smart design and then you save your optimization time for actual performance problems in your working app and then you measure carefully to understand where you can make a difference. Premature optimization is wasted time and usually targeted at the wrong thing which just makes your code take longer to write, more complicated to get right and larger without actually being focused in the right areas.jfriend00– jfriend002013年02月04日 01:41:46 +00:00Commented Feb 4, 2013 at 1:41
3 Answers 3
Don't forget that your array doesn't work unless you define each of those variable. But as I understand, those are words, not vars. So the right array declaration is:
var newGameItem = ["cattle", "wood", "stone", "metal", "grain", "craft"]
To repeat that array 10 times, you can use a for loop and the array method concat().
var newGameItem = [];
for(var i=0 ; i<10 ; i++) {
newGameItem = newGameItem.concat(gameItem);
}
> newGameItem : ["cattle", "wood", "stone", "metal", "grain", "craft", "cattle" ... ]
I don't know what is your idea for this, but, if you want to store a quantity of elements of those kinds, like, player has 1 cattle, 12 wood, 20 stone, etc ... You probably should have a better data structure, like this:
var gameItem = {"cattle" : 1, "wood" : 12, "stone" : 20, "metal" : 0, "grain" : 0, "craft" : 0 };
So by doing gameItem.wood, you get the result > 12
1 Comment
You just need a couple loops:
function dupElements(arr, n) {
for (var i = 0, len = arr.length; i < len; i++) {
for (var j = 0; j < n; j++) {
arr.push(arr[i]);
}
}
}
dupElements(gameItem, 10);
The outer loop cycles through the initial elements. The inner loop adds n copies of each.
Comments
You can do:
var gameItem = ['cattle', 'wood', 'stone', 'metal', 'grain', 'craft'];
var doublearr = gameItem.concat(gameItem); //x2
gameItem = doublearr.concat(doublearr);//x4
gameItem = gameItem.concat(gameItem );//x8
gameItem = gameItem.concat(doublearr);//x10
alert(gameItem);
Concatenate the array to itself until you reach the closest ^2 (below - in our case 8 = 2^3) and then add the rest.
I posted this solution because you wrote in one of the comments that you hope for something that isn't performance intensive. But I agree with the others that it's more readable and easier to maintain a simple for-loop. So if you have only x10 - keep it simple.