我有一个如下所示的对象数组:/image/jaVcy.png
每个条目都是该数组中的一个对象。我需要做的是随机化每个不是标题的元素的顺序。每个标题必须保留在初始索引,但两个标题之间的元素必须是随机的。附图中描述了它的外观。
标题和常规元素之间的唯一区别是它的值是一个正则表达式,看起来像这样 #H#[0-9]+
所以,我所做的是: 我遍历数组,记下每个标题的索引。
然后,迭代索引数组并将该数组拆分为多个较小的数组(每个标题一组)。
然后,再次迭代包含拆分数组的数组,从索引 0 开始拼接每个数组(删除标题元素),打乱这些值,取消移动数组并在开头添加标题元素。
最后,将 splittedArrayOfArrays 中的所有数组连接成我需要的数组,即 current.choices。
执行三次迭代在性能方面似乎不太明智,是否有其他可能的方法仅随机化数组中的元素组?
这是我编写的代码以使其工作:
var headlineIndexes = [];
var splittedArrayOfArrays = [];
for (var ii = 0; ii < current.choices.length; ii++) {
if (regex.test(current.choices[ii].value)) {
headlineIndexes.push(ii);
}
}
for (var ii = 0; ii < headlineIndexes.length; ii++) {
//if there is another headlineIndex, split Array until that index
if (headlineIndexes[ii + 1]) {
splittedArrayOfArrays[ii] = current.choices.slice(headlineIndexes[ii], headlineIndexes[ii + 1])
}
//if not, split until end of array
else {
splittedArrayOfArrays[ii] = current.choices.slice(headlineIndexes[ii]);
}
}
current.choices = [];
for (var ii = 0; ii < splittedArrayOfArrays.length; ii++) {
//remove first element and store in firstElem
var firstElem = splittedArrayOfArrays[ii].splice(0, 1);
//call shuffle with remaining elements, which shuffles the elements WITHOUT the headline
shuffle(splittedArrayOfArrays[ii]);
// re-add the headline as first elem of splittedArray
splittedArrayOfArrays[ii].unshift(firstElem[0]);
}
current.choices = splittedArrayOfArrays.reduce( function(prev, next) {
return prev.concat(next) ;
});
编辑:我意识到没有理由迭代 splittedArrayOfArrays,一切都可以从第二个 for 循环完成。我认为这对于数组中最多 40 个元素来说已经足够有效了。这是最终的代码:
var headlineIndexes = [];
var splittedArrayOfArrays = [];
//save indexes at which we have headlines
for (var ii = 0; ii < current.choices.length; ii++) {
if (regex.test(current.choices[ii].value)) {
headlineIndexes.push(ii);
}
}
//split choices array into groups for each headline.
for (var ii = 0; ii < headlineIndexes.length; ii++) {
//if we have another headline, make new array with elements from current index to next index
if (headlineIndexes[ii + 1]) {
splittedArrayOfArrays[ii] = current.choices.slice(headlineIndexes[ii], headlineIndexes[ii + 1])
}
//else, new array from current index to end of choices array
else {
splittedArrayOfArrays[ii] = current.choices.slice(headlineIndexes[ii]);
}
//remove first element which is the headline, store in firstElem
var firstElem = splittedArrayOfArrays[ii].splice(0, 1);
//shuffle the choices of the group
shuffle(splittedArrayOfArrays[ii]);
//add the first element back to the first position of the group
splittedArrayOfArrays[ii].unshift(firstElem[0]);
}
//delete choices array
current.choices = [];
//concatenate group arrays into the choices array
current.choices = splittedArrayOfArrays.reduce( function(prev, next) {
return prev.concat(next) ;
});
最佳答案
Performing three iterations doesn't seem very wise in performance [...]
我不会担心这个。除非有数千个标题需要分组以及数十万个元素需要洗牌,否则此例程根本不会影响性能。
如果您确实想调整它,您可以就地进行洗牌,即在原始数组内,以避免必须复制数组并将它们再次放在一起。
关于javascript - 如何有效地随机化索引之间的多个数组元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43737263/