我有一个看起来像这样的对象:https://i.stack.imgur.com/javcy.png
每个条目是该数组中的对象。我需要做的是将不是标题的每个元素的顺序随机化。每个标题必须保留在初始索引上,但是两个标题之间的元素必须随机化。在附带的图片中是对它的外观的描述。
标题和常规元素之间的唯一区别是,其值是看起来像这样的#H#[0-9]
所以,我所做的是:我通过数组迭代,注意每个标题的索引。
然后,通过索引数组迭代并将数组分为多个较小的数组(每个标题为一组(。
然后,再次通过包含拆分数组的数组再次迭代,将每个数组从索引0开始(删除标题元素(开始,然后缩短这些值,取消换档数组,然后在开始时添加标题元素。
最后,将所有阵列中的所有阵列加入我需要的数组,即当前。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) ;
});
编辑:我意识到没有理由迭代拆分arrayofarrays,从第二个循环开始,一切都可以完成。我认为这是有效的,对于无论如何,最多可以在阵列中拥有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) ;
});
执行三个迭代似乎在性能[...]
方面似乎不是很明智
我不用担心。除非有成千上万的头条新闻和数十万个要素的要素,否则这种例行程序根本不会影响性能。
如果您真的想对其进行调整,则可以在原始数组中进行洗牌,以免复制数组并再次将它们放在一起。
我意识到没有理由迭代拆分arrayofarrays,从第二个循环开始,一切都可以完成。我认为这是有效的,对于无论如何,最多可以在阵列中拥有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) ;
});