“交易” JavaScript 中的数组元素

发布于 2024-12-14 22:22:30 字数 336 浏览 0 评论 0 原文

我有四个数组(array1..4),每个数组包含四个字符串,例如

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

我还有四个空数组 (finalarray1 ..4),其中我想从四个原始数组中弹出()一个随机选择的项目,这样我就会有四个像这样的数组

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

这是如何在 JavaScript 中完成的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时常饿 2024-12-21 22:22:30

如果您希望最终数组是唯一的(初始数组中的每个元素在所有最终数组中仅使用一次),请使用

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] );
  }
}

http://jsfiddle.net/w4nWU/


我没有使用多个变量,而是使用了两个数组来保存初始数组和最终数组。

无论有多少个变量,上述方法都将起作用。排列你并且无论数组中有多少元素(假设它们同样长)。

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).

疯狂的代价 2024-12-21 22:22:30

一种方式(为了方便起见,将 4 个输出数组存储在另一个数组中,销毁传递的输入)

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文