1

I have the following code:

function build_all_combinations(input_array){
 array = [1,2,3]
 limit = array.length - 1
 combo = []
 for(i = 0; i<= limit; i++){
 splice_value = array.splice(0,1)
 push_value = splice_value[0]
 array.push(push_value)
 console.log(array) 
 combo.push(array) 
 }
 console.log(combo) 
}

Which outputs:

[2, 3, 1]
[3, 1, 2]
[1, 2, 3]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]

The last line should be: [[2, 3, 1],[3, 1, 2],[1, 2, 3]]

I'm obviously not grokking something about the way the array is working. Each individual array is correct, but when I go to push them to the combo array, something is failing along the way. What is it?

asked Jun 27, 2012 at 16:38
2
  • 1
    Are those variables defined somewhere else? what about i? it looks like it's global. Try making i local with var -> for(var i=0... Commented Jun 27, 2012 at 16:42
  • Do you mean to push push_value into combo as well? Commented Jun 27, 2012 at 16:44

3 Answers 3

5

Each time you are pushing the same array into the combo array; ie, all the references are pointing to the same instance in memory. So when you update one, you've in reality updated them all.

If you want separate references, you'll need to create separate arrays.

answered Jun 27, 2012 at 16:43

2 Comments

I can use array.new() but then how do I set it equal to another array without setting the same instance in memory?
@NoahClark easiest way to make a copy of array is var copy = old.slice(0);
3

shift and slice are your friends here:

var array = [1,2,3];
var combo = []
for(var i = 0; i<array.length; i++){
 array.push(array.shift()); // remove first element (shift) and add it to the end (push)
 combo.push(array.slice()); // add a copy of the current array to combo
}

DEMO

answered Jun 27, 2012 at 17:04

1 Comment

I've added this to the solution!
2

jordan002 has given the explanation for this behaviour.

Here's the workaround.

array = [1,2,3]
limit = array.length - 1
combo = []
for(i = 0; i<= limit; i++){
 splice_value = array.splice(0,1)
 push_value = splice_value[0];
 array.push(push_value);
 console.log(array);
 combo.push(array.concat([]));
}
console.log(combo); 

You can store a copy in temp variable.

array = [1,2,3]
limit = array.length - 1
combo = []
for(i = 0; i<= limit; i++){
 splice_value = array.splice(0,1)
 push_value = splice_value[0];
 array.push(push_value);
 console.log(array);
 var temp = array.slice(); 
 combo.push(temp);
}
console.log(combo) 

Reference

answered Jun 27, 2012 at 16:48

2 Comments

This works! Would you be able to use array.new some how instead of concat? That seems more explicit about what you're doing.
I did the same few hours back :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.