创建多维键值数组javascript

时间:2017-04-05 06:56:14

标签: javascript jquery arrays multidimensional-array

我有一个具有以下结构的数据表。

Datatable structure

我想创建一个键值多维数组。即

main_array = {
  user_id  =  {
    'name' : 'XYZ',
    'email' : 'xyz@com'
  }

包含列的复选框如下:

<input type="checkbox" name="id" value="3" data-name="xyz" data-email="xyz@com">

我创建的脚本到现在为止:

 var rows_selected = [];
 var $i = 0;    
 $("input:checked", rows).each(function(){
     var val = $(this).val();
    if($.inArray($(this).val(),rows_selected) == -1 ){
         rows_selected.push($(this).val());
         rows_selected[$i][val] = [];
         rows_selected[$i][val].push({'name':$(this).attr('data-name'),'email':$(this).attr('data-email')});
    }
    $i++;
 });

但是这个脚本总是会抛出一个错误:无法读取未定义的属性'push'

那么可以在javascript中创建一个键值对,它将user_id作为键和名称,将电子邮件数组作为其值导入主结果数组中吗?我们还可以使用javascript拼接那个特定的密钥吗?

感谢任何帮助。感谢

2 个答案:

答案 0 :(得分:1)

我不得不在这里猜一下,但如果您的目标是拥有一个对象,其属性由$(this).val()命名,其值为具有nameemail属性的对象,然后:

var rows_selected = [];
$("input:checked", rows).each(function(){
    var $this = $(this);
    var entry = {};
    entry[$this.val()] = {
        name: $this.attr('data-name'),
        email: $this.attr('data-email')
    };
    rows_selected.push(entry);
});

我认为没有必要进行inArray检查(并且无法理解您尝试使用它进行检查)。

示例:

$("#btn").on("click", function() {
  var rows_selected = [];
  $("input:checked"/*, rows*/).each(function(){
      var $this = $(this);
      var entry = {};
      entry[$this.val()] = {
          name: $this.attr('data-name'),
          email: $this.attr('data-email')
      };
      rows_selected.push(entry);
  });
  console.log(rows_selected);
});
<div>
  <label>
    <input type="checkbox" value="one" data-name="name for one" data-email = "email for one">
    One
  </label>
</div>
<div>
  <label>
    <input type="checkbox" value="two" data-name="name for two" data-email = "email for two">
    Two
  </label>
</div>
<div>
  <label>
    <input type="checkbox" value="three" data-name="name for three" data-email = "email for three">
    Three
  </label>
</div>
<input type="button" id="btn" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

但是如果你不需要一个数组,只需要一个带有$(this).val()键的对象,它就更简单 - 无论是构建还是以后消耗:

var rows_selected = {};
$("input:checked", rows).each(function(){
    var $this = $(this);
    rows_selected[$this.val()] = {
        name: $this.attr('data-name'),
        email: $this.attr('data-email')
    };
});

示例:

$("#btn").on("click", function() {
var rows_selected = {};
$("input:checked"/*, rows*/).each(function(){
  var $this = $(this);
  rows_selected[$this.val()] = {
     name: $this.attr('data-name'),
     email: $this.attr('data-email')
    };
  });
  console.log(rows_selected);
});
<div>
  <label>
    <input type="checkbox" value="one" data-name="name for one" data-email = "email for one">
    One
  </label>
</div>
<div>
  <label>
    <input type="checkbox" value="two" data-name="name for two" data-email = "email for two">
    Two
  </label>
</div>
<div>
  <label>
    <input type="checkbox" value="three" data-name="name for three" data-email = "email for three">
    Three
  </label>
</div>
<input type="button" id="btn" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

我认为您需要一组对象,例如:

&#13;
&#13;
var rows_selected = [];
var $i = 0;
$("input:checked").each(function(){
    var val = $(this).val();
    var attrs = {'name':$(this).attr('data-name'),'email':$(this).attr('data-email')};
    rows_selected[$i++] = attrs;
 });
console.log(rows_selected);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="id" value="1" data-name="xyz" data-email="xyz@com" checked="checked">
<input type="checkbox" name="id" value="2" data-name="abc" data-email="abc@com" checked="checked">
<input type="checkbox" name="id" value="3" data-name="jjj" data-email="jjj@com" checked="checked">
&#13;
&#13;
&#13;

如果使用多维数组,例如:

rows_selected[$i][val] = [];

你可以得到一个数组&#34;空&#34;元素,JavaScript中的到期数组是&#34;有序&#34;,请参阅以下示例:

&#13;
&#13;
var test = [];

test[0] = "zero";
test[4] = "four";

console.log(test);
&#13;
&#13;
&#13;

我希望它可以帮助你,再见。