如何有效地随机化索引之间的多个数组元素?

How to efficiently randomize multiple array elements between indexes?

我有一个 object 数组,如下所示:https://i.stack.imgur.com/jaVcy.png

每个条目都是该数组中的一个 object。我需要做的是随机化每个不是标题的元素的顺序。每个标题必须保留在初始索引处,但两个标题之间的元素必须是随机的。所附图片描述了它的外观。

标题和常规元素之间的唯一区别是它的值是一个看起来像这样的正则表达式 #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 [...]

我不担心这个。除非有数千个标题要分组和数十万个元素要混洗,否则此例程根本不会影响性能。

如果你真的想调整它,你可以就地洗牌,意思是在原始数组内部,以避免必须复制数组并将它们再次放在一起。

我意识到没有理由迭代 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) ;
                    });