2

I have four arrays(array1..4) each containing four strings e.g

var array1 = ['array1item1', 'array1item2', 'array1item3', 'array1item4']

I also have four empty arrays (finalarray1..4) in which I want to pop() a randomly selected item from each my four original arrays so I would have four arrays like this like this

finalarray1 = ['array1item3', 'array2item2', 'array3item2','array4item1']

How is this accomplished in javascript?

2
  • Do they need to be unique per output array? Commented Nov 9, 2011 at 12:53
  • yes, it takes one element from each inital array to create a final array Commented Nov 9, 2011 at 12:56

2 Answers 2

1

If you want the final arays to be unique (each element from the initial arrays is only used once in all the final arrays) use

function iRandomUpTo(upto){
  return Math.floor( Math.random()*(upto) );
}
var initialArrays = [
    ['array1item1','array1item2','array1item3','array1item4'],
    ['array2item1','array2item2','array2item3','array2item4'],
    ['array3item1','array3item2','array3item3','array3item4'],
    ['array4item1','array4item2','array4item3','array4item4']
];

var finalArrays = [];

for(arrayIndex in initialArrays){

  var newArray = finalArrays[arrayIndex] = [];
  for(idx in initialArrays){
    var singleArray = initialArrays[ idx ];
    newArray.push( singleArray.splice( iRandomUpTo( singleArray.length ), 1 )[0] );
  }
}

Demo at http://jsfiddle.net/w4nWU/


Instead of using multiple variables I used two arrays that hold the initial and final arrays..

The above method will work regardless of the number of arrays you have and regardless of the number of elements you have in your arrays (assuming they are equally long).

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

Comments

0

One way (stores the 4 output arrays in another array for convenience, destroys the passed inputs)

var array1 = ['array1item1', 'array1item2', 'array1item3', 'array1item4']
var array2 = ['array2item1', 'array2item2', 'array2item3', 'array2item4']
var array3 = ['array3item1', 'array3item2', 'array3item3', 'array3item4']
var array4 = ['array4item1', 'array4item2', 'array4item3', 'array4item4']

finalarrays = shuffle(array1, array2, array3, array4);

function shuffle() {
    var index, results = [];
    for (var output = 0; output < arguments.length; output++) {
        results[output] = [];
        for (var input = 0; input < arguments.length; input++) {
            results[output].push(arguments[input].splice(Math.floor(Math.random() * arguments[input].length), 1));
        }
    }
    return results;
}

print( finalarrays[0].join() )
print( finalarrays[1].join() )
print( finalarrays[2].join() )
print( finalarrays[3].join() )

>> array1item2,array2item1,array3item2,array4item4
>> array1item4,array2item4,array3item4,array4item2
>> array1item1,array2item3,array3item3,array4item1
>> array1item3,array2item2,array3item1,array4item3

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.