Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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 如何使用jquery创建动态关联数组?_Javascript_Jquery_Html_Arrays - Fatal编程技术网

Javascript 如何使用jquery创建动态关联数组?

Javascript 如何使用jquery创建动态关联数组?,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我想要这样的数组:- arr[0][from] = value arr[0][to] = value arr[1][from] = value arr[1][to] = value 。 . 等等 我为from&to字段输入了数组html元素 <input type="text" name="from[]" value=""/> <input type="text" name="to[]" value=""/> 即使我写arr[index]['from']=from而

我想要这样的数组:-

arr[0][from] = value
arr[0][to] = value

arr[1][from] = value
arr[1][to] = value
。 . 等等

我为from&to字段输入了数组html元素

<input type="text" name="from[]" value=""/>
<input type="text" name="to[]" value=""/>
即使我写
arr[index]['from']=from
而不是
arr[index].push({from:from})然后也给出错误


请任何人帮我解决这个问题。提前感谢。

如果索引处的对象未定义,则需要推送,否则请更新它。您不需要使用
typeof

var arr = [];
$('input[name^=from]').each(function(index,val) {
    var from = $(this).val();
    if (arr[index] === undefined) {
        arr[index] = {from: from};
    } else {
        arr[index].from = from;
    }
});
$('input[name^=to]').each(function(index,val) {
    var to= $(this).val();
    if (arr[index] === undefined) {
        arr[index] = {to: to};
    } else {
        arr[index].to = to;
    }
});
var arr=[];
$('input[name^=from]')。每个(函数(索引,val){
var from=$(this.val();
if(arr[索引]==未定义){
arr[索引]={
发件人:发件人
};
}否则{
arr[index].from=from;
}
});
$('input[name^=to]')。每个(函数(索引,val){
var to=$(this.val();
if(arr[索引]==未定义){
arr[索引]={
致:致
};
}否则{
arr[index].to=to;
}
});
控制台日志(arr)

如果
索引中的对象未定义,则需要推送,否则需要更新它。您不需要使用
typeof

var arr = [];
$('input[name^=from]').each(function(index,val) {
    var from = $(this).val();
    if (arr[index] === undefined) {
        arr[index] = {from: from};
    } else {
        arr[index].from = from;
    }
});
$('input[name^=to]').each(function(index,val) {
    var to= $(this).val();
    if (arr[index] === undefined) {
        arr[index] = {to: to};
    } else {
        arr[index].to = to;
    }
});
var arr=[];
$('input[name^=from]')。每个(函数(索引,val){
var from=$(this.val();
if(arr[索引]==未定义){
arr[索引]={
发件人:发件人
};
}否则{
arr[index].from=from;
}
});
$('input[name^=to]')。每个(函数(索引,val){
var to=$(this.val();
if(arr[索引]==未定义){
arr[索引]={
致:致
};
}否则{
arr[index].to=to;
}
});
控制台日志(arr)


这很难。使用普通HTML是不可能的。您需要使用JavaScript手动分配索引。这是JavaScript而不是html。是的,我知道。。。我在这里也做过类似的事情:这很难。使用普通HTML是不可能的。您需要使用JavaScript手动分配索引。这是JavaScript而不是html。是的,我知道。。。我在这里也做了类似的事情:我在控制台“无法从未定义的”设置属性时遇到此错误。我在控制台“无法从未定义的”设置属性时遇到此错误。