附加到二维数组的Javascript

附加到二维数组的Javascript,javascript,arrays,Javascript,Arrays,我正在尝试将数组附加到数组中。我希望输出类似于: [[Dep,POR],[14073,99.25],[14072,0.06]] 但我得到了: Dep,POR,14073,99.25,14072,0.06 以下是我目前掌握的情况: function get_historical() { var well = document.getElementById('wellSelect'); var selected_well = well.options[well.selecte

我正在尝试将数组附加到数组中。我希望输出类似于:

[[Dep,POR],[14073,99.25],[14072,0.06]]

但我得到了:

Dep,POR,14073,99.25,14072,0.06
以下是我目前掌握的情况:

  function get_historical() {
    var well = document.getElementById('wellSelect'); 
    var selected_well = well.options[well.selectedIndex].value; 
    var hist_json_obj = JSON.parse(Get("/get_historical/" + selected_well));
    hist_por = ['Dep','POR'];
    for (var item in hist_json_obj) {
      if (hist_json_obj.hasOwnProperty(item)) {
         var dep = hist_json_obj[item].dep;
         var por = hist_json_obj[item].por;

         var arr_por = [dep, parseFloat(por)];
         hist_por.push(arr_por);

      }
    }
    document.write(hist_por);
  }

当您初始化
hist_por
时,您希望它是一个二维数组,而当前只有一个数组。因此,您希望将其实例化更改为:

hist_por = [['Dep','POR']]; // [[ ... ]] instead of [ ... ]
同样根据@justrusty的回答,当您将JSON.stringify(hist_por)传递到
document.write()时,您需要
JSON.stringify(hist_por)
。这是更重要的部分,所以他的回答应该被接受

因此,整个代码块将变成:

function get_historical() {
  var well = document.getElementById('wellSelect'); 
  var selected_well = well.options[well.selectedIndex].value; 
  var hist_json_obj = JSON.parse(Get("/get_historical/" + selected_well));
  hist_por = [['Dep','POR']];
  for (var item in hist_json_obj) {
    if (hist_json_obj.hasOwnProperty(item)) {
       var dep = hist_json_obj[item].dep;
       var por = hist_json_obj[item].por;

       var arr_rop = [dep, parseFloat(por)];
       hist_por.push(arr_por);

    }
  }
  document.write(JSON.stringify(hist_por));
}

当您初始化
hist_por
时,您希望它是一个二维数组,而当前只有一个数组。因此,您希望将其实例化更改为:

hist_por = [['Dep','POR']]; // [[ ... ]] instead of [ ... ]
同样根据@justrusty的回答,当您将JSON.stringify(hist_por)
传递到
document.write()时,您需要
JSON.stringify(hist_por)
。这是更重要的部分,所以他的回答应该被接受

因此,整个代码块将变成:

function get_historical() {
  var well = document.getElementById('wellSelect'); 
  var selected_well = well.options[well.selectedIndex].value; 
  var hist_json_obj = JSON.parse(Get("/get_historical/" + selected_well));
  hist_por = [['Dep','POR']];
  for (var item in hist_json_obj) {
    if (hist_json_obj.hasOwnProperty(item)) {
       var dep = hist_json_obj[item].dep;
       var por = hist_json_obj[item].por;

       var arr_rop = [dep, parseFloat(por)];
       hist_por.push(arr_por);

    }
  }
  document.write(JSON.stringify(hist_por));
}
这可能对你有帮助

var-arr=['foo','bar'];
var arr2=['baz','boo']
arr.push(arr2);
控制台日志(arr);
文件编写(arr);
文件。写(“
”); document.write(JSON.stringify(arr));
这基本上就是它写入文档的方式。如果您在控制台中登录它,您将看到附加的数组。或者,如果您先
JSON.stringify()
,它将按照您的预期显示

我的建议是始终
console.log()
,这样您就可以确切地看到数据的结构了这可能会对您有所帮助

var-arr=['foo','bar'];
var arr2=['baz','boo']
arr.push(arr2);
控制台日志(arr);
文件编写(arr);
文件。写(“
”); document.write(JSON.stringify(arr));
这基本上就是它写入文档的方式。如果您在控制台中登录它,您将看到附加的数组。或者,如果您先
JSON.stringify()
,它将按照您的预期显示


我的建议是始终
console.log()
,这样您就可以准确地看到数据的结构了其他人已经指出了问题所在(+您的一个变量名中有一个输入错误-
arr_rop
vs
arr_por
)。以下是一个ES6版本,它将突破旧浏览器,用于学习:

function get_historical() {
  const well = document.getElementById('wellSelect');
  const selected_well = well.options[well.selectedIndex].value;
  const hist_json_obj = JSON.parse(Get("/get_historical/" + selected_well));

  const hist_por = Object.values(hist_json_obj).reduce(
    (arr, item) => [...arr, [item.dep, +item.por]],
    [["Dep", "POR"]]
  );

  document.write(JSON.stringify(hist_por));
}

其他人已经指出了问题所在(+您的一个变量名中有一个输入错误-
arr\u rop
vs
arr\u por
)。以下是一个ES6版本,它将突破旧浏览器,用于学习:

function get_historical() {
  const well = document.getElementById('wellSelect');
  const selected_well = well.options[well.selectedIndex].value;
  const hist_json_obj = JSON.parse(Get("/get_historical/" + selected_well));

  const hist_por = Object.values(hist_json_obj).reduce(
    (arr, item) => [...arr, [item.dep, +item.por]],
    [["Dep", "POR"]]
  );

  document.write(JSON.stringify(hist_por));
}

谢谢修复了它。你甚至不需要将它实例化为2D,它实际上就是用document.write()打印出来的方式。我的建议是总是登录到console,这样您就可以看到真正的价值观以及它们是如何组织的。谢谢。修复了它。你甚至不需要将它实例化为2D,它实际上就是用document.write()打印出来的方式。我的建议是总是登录到console,这样您就可以看到真正的值以及它们是如何组织的。