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

Javascript 如何有效地在索引之间随机化多个数组元素?,javascript,arrays,random,concat,Javascript,Arrays,Random,Concat,我有一个如下所示的对象数组: 每个条目都是该数组中的一个对象。我需要做的是随机排列不是标题的每个元素的顺序。每个标题必须保持在初始索引,但两个标题之间的元素必须随机化。附件中的图片描述了它应该是什么样子 标题和常规元素之间的唯一区别在于,它的值是一个正则表达式,看起来像这样#H#[0-9]+ 所以,我所做的是: 我遍历数组,记录每个标题的索引 然后,迭代索引数组并将数组拆分为多个较小的数组(每个标题一组) 然后,再次迭代包含拆分数组的数组,从索引0开始拼接每个数组(删除headline元素),洗

我有一个如下所示的对象数组:

每个条目都是该数组中的一个对象。我需要做的是随机排列不是标题的每个元素的顺序。每个标题必须保持在初始索引,但两个标题之间的元素必须随机化。附件中的图片描述了它应该是什么样子

标题和常规元素之间的唯一区别在于,它的值是一个正则表达式,看起来像这样#H#[0-9]+

所以,我所做的是: 我遍历数组,记录每个标题的索引

然后,迭代索引数组并将数组拆分为多个较小的数组(每个标题一组)

然后,再次迭代包含拆分数组的数组,从索引0开始拼接每个数组(删除headline元素),洗牌这些值,取消移位数组并在开头添加headline元素

最后,将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) ;
                    });
var headlineindex=[];
var splittedArrayOfArrays=[];
对于(var ii=0;ii
编辑:我意识到没有理由迭代分割的ArrayOfarray,一切都可以从第二个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) ;
                    });
                    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) ;
                    });
var headlineindex=[];
var splittedArrayOfArrays=[];
//保存标题所在的索引
对于(var ii=0;ii
执行三次迭代在性能上似乎不是很明智[…]

我不会担心的。除非有成千上万的标题要分组,数十万个元素要洗牌,否则这个例行程序根本不会影响性能


如果你真的想调整它,你可以在原来的数组中进行适当的洗牌,以避免复制数组并将它们重新组合在一起。

我意识到没有理由对分割的数组进行迭代,一切都可以从第二个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) ;
                    });
                    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) ;
                    });
var