我有两个数组,就像是
var array1=['a1','a2','a3','a4','a5,','a6','a7','a8','a9','a10,','a11','a12'----];
var array2=['b1','b2','b3','b4',----];
var result= [];
我想将结果数组显示为
result= ['a1','a2','b1','a3','a4,','a5','a6','a7','a8','a9,','a10','a11','b2','a12']
第二个数组'array2'值位于3,12,19,28,35 ...(系列如3,3 + 9,12 + 7,19 + 9,28 + 7等) 如果'array1'的值小于'array2',那么值应该连续放在'result []'中,例如:
array1=['a1','a2','a3'];
array2=['b1','b2','b3'];
然后
result=['a1','a2','b1','a3','b2','b3'];
如何使用jquery实现这一点?
答案 0 :(得分:1)
快速管理此方法。但它可以进行优化。
var array1=['a1','a2','a3','a4','a5,','a6','a7','a8','a9','a10,','a11','a12'];
var array2=['b1','b2','b3','b4'];
document.body.innerHTML += "arr1:" + array1 + "<br/>";
document.body.innerHTML += "arr2:" + array2 + "<br/>";
var result= [];
var index=0;
var lastInsertedBindex=0;
var step1 = 9;
var step2 = 7;
var lastUsedStep = step2;
var nextStepIndex = 3;
elements = true;
var indexFromA = 0;
var indexFromB = 0;
var elementsExists = true;
do{
if( index != nextStepIndex ){
if( undefined != array1[indexFromA] ){
result.push( array1[indexFromA]);
indexFromA++;
} else if( undefined != array2[indexFromB]){
result.push( array2[indexFromB]);
indexFromB++;
} else
elementsExists = false;
index++;
continue;
}else{
if( undefined != array2[indexFromB] ){
result.push(array2[indexFromB]);
indexFromB++;
lastUsedStep = lastUsedStep == step1 ? step2 : step1;
nextStepIndex = nextStepIndex + lastUsedStep;
} else if( undefined != array1[indexFromA]) {
result.push(array1[indexFromA]);
indexFromA++;
} else
elementsExists = false;
index++;
}
}while(elementsExists);
document.body.innerHTML += "res:" + result;
答案 1 :(得分:0)
这应该有用。
// Create arrays
var array1 = [];
var array2 = [];
for(i=1;i<100;i++){
array1[array1.length] = "a"+i;
}
for(i=2;i<50;i++){
array2[array2.length] = "b"+i;
}
// The function
function coupleArrays(array1, array2){
var newArray = [];
var jump = 9; // first jump
var pos = 3; // Starting point
while(array1.length > 0){
var i = newArray.length;
if(i == pos-1 && array2.length != 0){
// remove first from array2 and add to newArray
newArray[i] = array2.shift();
pos += jump; // add the jump to get new position
// set jump to 9 or 7
if(jump == 9) jump = 7;
else jump = 9;
}else{
// remove first from array1 and add to newArray
newArray[i] = array1.shift();
}
}
return newArray;
}
var coupled = coupleArrays(array1, array2);
// Just to show output
for(i=0;i<coupled.length;i++){
var nr = i;
if( nr < 10 ) nr = "0"+nr;
if( nr < 100 ) nr = "0"+nr;
document.write( nr + ": " + coupled[i] + "<br />");
}
&#13;
答案 2 :(得分:0)
你可以做的一种方法是循环<h:form id="saveForm" styleClass="form">
<h:inputText value="#{wordController.currentWord}" id="words" />
<h:commandButton action="#{wordController.addWord()}" type="submit" value="+">
<f:ajax render="wordlist" />
</h:commandButton>
<h:panelGroup id="wordlist">
....
,将前x个项切片到结果(取决于它可能是3,7或9的迭代)并附加{{1的当前索引}}。然后,一旦array2
中的元素用尽,继续添加array2
中的元素。
array1
array2
答案 3 :(得分:0)
您可以使用递归函数来解决此问题:
var array1 = [];
var array2 = [];
// Preparing data
for (var i = 0, len = 100; i < len; ++i) {
array1[i] = 'a' + (i + 1);
array2[i] = 'b' + (i + 1);
}
var index = 3;
function insert (arr1, arr2, i) {
// Insert element to array
array1.splice(index - 1, 0, array2[i]);
index = ((index % 2) == 1) ? index + 9 : index + 7;
if ( index < arr2.length - 1) { insert(arr1, arr2, i + 1) }
return;
}
insert(array1, array2, 0);
array1.forEach( function (item) {
let output = (item.indexOf('b') !== -1) ? '<b>' + item + ' </b>'
: item + ' ';
$('#result').append(output);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="result"></div>