Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中使用for循环成对检索数组元素?_Javascript_Arrays_For Loop - Fatal编程技术网

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

如何在javascript中使用for循环成对检索数组元素?,javascript,arrays,for-loop,Javascript,Arrays,For Loop,我创建了一个空数组,该数组必须由网站用户用输入表单填充。用户可以输入他/她想要的任意数量的元素(在本例中是朋友),但总数必须是偶数。 在数组中使用sort()方法(对输入设置的初始顺序进行洗牌)后,我需要将其元素配对并在站点上打印。 我尝试使用for循环来实现这一点,但一次只能检索一个元素。 有办法吗? 提前谢谢 var lista = []; function muestraNombres(){ var x = $("#amigo").val(); if(x == ""){

我创建了一个空数组,该数组必须由网站用户用输入表单填充。用户可以输入他/她想要的任意数量的元素(在本例中是朋友),但总数必须是偶数。 在数组中使用sort()方法(对输入设置的初始顺序进行洗牌)后,我需要将其元素配对并在站点上打印。 我尝试使用for循环来实现这一点,但一次只能检索一个元素。 有办法吗? 提前谢谢

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();
}
var lista=[];
函数muestraNombres(){
var x=$(“#amigo”).val();
如果(x==“”){
警报(“安格拉边洛斯达托斯”);
}否则{
lista.push(x);
$(“#listado”)。追加(“”+x+“”);
$(“#朋友”).val(“”);
}
}
函数递归(){
如果(列表长度%2!=0){
警报(“Debes Ingrear otro amigo para Realizer los pares”);
}否则{
朋友秘书();
}
}
函数amigoSecreto(){
$(“#listado”).hide();
洗牌();
generaPares();
}
函数shuffle(){
sort(函数(){return 0.5-Math.random()})
}
函数generaPares(){
for(变量i=0;i
for(变量idx=0;idx
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 "
var-friendsArray=[];
//让我们在数组中放入一些值
//使用简单对象表示法
push({name:'stan',friendsname:'kyle'});
push({name:'stan',friendsname:'eric'});
推送({name:'buttes',friendsname:'kenny'});
//让我们打印数组
对于(var i=0;i
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 "