0

I need help with the five.myArraysCombined property.

I need it to equal just 1 array (which it currently does in fiddle) and I need it to NOT add any numbers together. (so each number in the array shouldn't be over 20, just like no number in the other arrays are over 20)

http://jsfiddle.net/Dc6HN/1/

For example, if the five arrays are like this

five.myArray1 = [7,2,9,19,3];
five.myArray2 = [6,18,8,1,7];
five.myArray3 = [7,19,4,8,2];
five.myArray4 = [11,9,1,14,5];
five.myArray5 = [3,18,8,9,2];

then the all those arrays combined should be like this

five.myArraysCombined = [7,2,9,19,3,6,18,8,1,7,7,19,4,8,2,11,9,1,14,5,3,18,8,9,2];

Relevant code :

function theNumberClass() {
    this.myArray = [[],[],[],[],[]];
    this.myArraysCombined = [];
}
var five = new theNumberClass();

function prePickNumbers(objName, theNum, theSumNum, theMaxNum, theMinNum) {
    var zzz = [];
    for (var x = 0; x < theNum; x += 1) {
    pickNumbers(objName.myArray[x], theNum, theSumNum, theMaxNum, theMinNum);
      zzz += objName.myArray[x];
    }
    objName.myArraysCombined.push(zzz);
}

prePickNumbers(five, 5, 40, 20, 1);

My latest attempt was with var zzz and then pushing it to the property, but when I do that it adds up the numbers in the array at times, which is not what I need.

I've also tried several attempts using the .concat(), but it seems to turn it into a string and sometimes also adds up the numbers.

4
  • You mean you have some arrays and you want to get one array with their elements ? Commented Jan 14, 2014 at 13:11
  • @dystroy I think so, but not sure what you mean. For this I have 5 arrays. Each array is comprised of 5 numbers. This is generated randomly. So once the 5 arrays are generated, I want to make 1 array that includes each number in the 5 arrays. Commented Jan 14, 2014 at 13:13
  • And do you want to remove duplicates ? Commented Jan 14, 2014 at 13:13
  • @dystroy The five.myArraysCombined should be 1 array that includes 25 numbers. Duplicates are fine in that array. Commented Jan 14, 2014 at 13:14

4 Answers 4

3

Suppose you have those arrays :

var a = [1, 2, 3]
var b = [4, 5, 6]
var c = [8]

Then you can get a merge of all those with

var all = [].concat.apply([],[a,b,c])

or with

var all = [a,b,c].reduce(function(merged, arr){ return merged.concat(arr) })

In both cases you get

[1, 2, 3, 4, 5, 6, 8]

The first solution is simpler, the second one is more extensible if you want, for example, to remove duplicate or do any kind of filtering/transformation.

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe I'm wrong, but second argument of reduce is useless, since there's no need to use initial value.
@AlienArrays This would work with a variable number of arrays as well.
2

I would guess that the issue is the "+=" operator. This operator is used to sum values, not add new elements to an array. Take the following line of code as an example:

zzz += objName.myArray[x];

What I am guessing is that "myArray[x]" is getting added to the value of zzz instead of getting appended to the end of the array. When adding elements to an array in javascript, push is the best option. A better way to write this line is:

zzz.push(objName.myArray[x]);

The question was a bit confusing so I'm not sure if this is what you are looking for but hopefully it will help anyways.

1 Comment

Yea, I guess it didn't work like I needed. My fault for testing the array incorrectly. I asked another question here stackoverflow.com/questions/21118706/… and got it fixed. Thanks though, and thanks for your time.
1
five.reduce(function(o,n){return o.concat(n)},[])

This will reduce the array to a single value, in this case an array of numbers. You can look up Array.reduce() on MDN for more info.

Comments

0

After many hours trying all suggestions left on this thread and another one, and trying multiple other things. I think I finally found a very simple way to do this. And it's the only way I tried that works 100% like I want.

http://jsfiddle.net/Dc6HN/2/

function prePickNumbers(objName, theNum, theSumNum, theMaxNum, theMinNum) {
    for (var x = 0; x < theNum; x += 1) {
    pickNumbers(objName.myArray[x], theNum, theSumNum, theMaxNum, theMinNum);
    objName.myArraysCombined.push(objName.myArray[x]);
}
   objName.myArraysCombined = objName.myArraysCombined.toString();
   objName.myArraysCombined = objName.myArraysCombined.split(',');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.