简体   繁体   English

如何使用javascript中的for循环成对检索数组元素?

[英]How do I retrieve array elements in pairs using the for loop in javascript?

I created an empty array which has to be filled by the website user with the input form. 我创建了一个空数组,网站用户必须使用输入表单来填充它。 The user can input any number of elements (in this case, friends) he/she wants, but the total has to be an even number. 用户可以输入他/她想要的任意数量的元素(在这种情况下为朋友),但总数必须为偶数。 After using the sort() method in the array (to shuffle the initial order set by the input), I need to form pairs with its elements and print it on the site. 在数组中使用sort()方法(以打乱由输入设置的初始顺序)之后,我需要与其元素形成对并在站点上打印出来。 I tried to do this using the for-loop but I can only retrieve one element at once. 我尝试使用for循环来执行此操作,但一次只能检索一个元素。 ¿Is there a way to do it? 有办法吗? Thanks in advance! 提前致谢!

var lista = [];

function muestraNombres(){
    var x = $("#amigo").val();

    if(x == ""){
        alert("ingresa bien los datos");
    }else{
        lista.push(x);

       $("#listado").append('<div id="otrodiv">' + x + '</div>');
       $("#amigo").val('');
    }
}


function recuperaNombres (){
    if (lista.length%2 != 0) {
        alert("Debes ingresar otro amigo para realizar los pares");
    }else{
        amigoSecreto();
    }
}

function amigoSecreto(){
    $("#listado").hide();
    shuffle();
    generaPares();
}

function shuffle (){
    lista.sort(function() {return 0.5 - Math.random() })
}

function generaPares (){
    for (var i=0; i<lista.length;i++){
        $("#resultado").append('<div id="otrodiv1">' + lista[i] + '</div>')
    }
    $("#reiniciar").show();
    $("#parear").hide();
    $("#ingresar").hide();
}
for (var idx=0; idx < arr.length; idx += 2) {
    arr[idx]; // is first element in pair
    arr[idx+1]; // is second element in pair
}

Javascript makes it very simple to create objects on-the-fly, you can do something like this : Javascript使动态创建对象变得非常简单,您可以执行以下操作:

var friendsArray = [];

// let's put some values in the array
// using plain simple object notation
friendsArray.push( { name : 'stan', friendName : 'kyle' } ) ;  
friendsArray.push( { name : 'stan', friendName : 'eric' } ) ;
friendsArray.push( { name : 'butters', friendName : 'kenny' } ) ;

// let's print the array
for (var i=0; i<friendsArray.length; i++) {
    var thisFriend = friendsArray[i];
    console.log(thisFriend.name + ' has ' + thisFriend.friendName + ' as a friend ');
}

// output is :
// "stan has kyle as a friend "
// "stan has eric as a friend "
// "butters has kenny as a friend "

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 for 循环将 javascript 数组的元素打印到网页? - How do I print the elements of a javascript array to the webpage using a for loop? 如何使用 for of loop javascript 返回数组中的元素数? - How do I return the number of elements in an array using for of loop javascript? 如何使用JQuery循环表元素和检索数据元素? - How do I loop through table elements AND retrieve data elements using JQuery? 如何在javascript中成对加入数组元素 - How to join array elements in pairs in javascript 如何在JavaScript中匹配二维数组中的对? - How do I match pairs in a 2 dimensional array in JavaScript? 如何使用 javascript 中的提示向数组添加新元素? - How do I add new elements to an array using a promt in javascript? 如何遍历 JavaScript object 中的相似键值对(a0,a1,a2)并生成一个没有键中数字的新数组(a)? - How do I loop through similar key value pairs(a0,a1,a2) in JavaScript object and generate a new array without the number in the key(a)? 使用 for 循环从 Array 中检索元素 - Using a for-loop to retrieve elements from an Array 如何在具有多个对象的数组中循环并仅使用Javascript列出某些元素? - How do I loop through an array with multiple objects and list certain elements in solely Javascript? 如何遍历一个数组(递归)并将对象键/值对推入另一个数组中? - How do I loop through an array (recurision) and push object key/value pairs in another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM