2

Having array of arrays

var a = [[[1, "alpha", "a"],
          [2, "beta",  "b"]],
         [[3, "gama",  "c"]],
         [[4, "delta", "d"]]];

var b = [];

1) How can I merge a[0] and a[2] into b?

2) How can I shuffle array b?


This is a shuffle algorithm I am using >>

Array.prototype.shuffle = function() {
  for (var i = 0; i < this.length; i++)
    this.push(this.splice(Math.random() * (this.length - i), 1)[0]);
  return this;
}

with syntax

myArray.shuffle();
000
  • 26,951
  • 10
  • 71
  • 101
Ωmega
  • 42,614
  • 34
  • 134
  • 203

4 Answers4

7

To merge you can simply use concat.

var b = a[0].concat(a[2]);

For shuffling you need to write your own shuffling logic. There is no API for such.

Shuffling -

Community
  • 1
  • 1
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
1
$.merge( a[0], b );
$.merge( a[2], b );
David G
  • 94,763
  • 41
  • 167
  • 253
1

You do not need jQuery specific function to do this

Look at http://w3schools.com/jsref/jsref_concat_array.asp

Joshua Wooward
  • 1,508
  • 2
  • 10
  • 12
0

"Shuffling" is pretty simple:

var arry = [0,1,2,3,4,5,6,7,8,9];
arry.sort(function(a,b){
    return Math.random() * 2-1;
});
Shmiddty
  • 13,847
  • 1
  • 35
  • 52
  • It'll work with any array of anything, but it's only pseudo random and consecutive "shuffles" have no effect since `Math.random()` is time-based – Shmiddty Oct 10 '12 at 21:42