简体   繁体   English

附加到二维数组的Javascript

[英]Javascript Appending to 2-D Array

I am trying to append an array to an array. 我正在尝试将数组追加到数组。 I am expecting the output to be something like: 我期望输出是这样的:

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

But I am getting: 但我得到:

Dep,POR,14073,99.25,14072,0.06

Here's what I have so far: 这是我到目前为止的内容:

  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);
  }

When you initialize hist_por , you want that to be a 2-D array whereas you currently have just a single array. 初始化hist_por ,您希望它是一个2-D数组,而当前只有一个数组。 So you would want to change its instantiation to: 因此,您需要将其实例更改为:

hist_por = [['Dep','POR']]; // [[ ... ]] instead of [ ... ]

Also per @justrusty's answer, you need to JSON.stringify(hist_por) when you pass it to document.write() . 同样根据@justrusty的回答,当您将其传递给document.write()时,您需要JSON.stringify(hist_por) document.write() This is the more important piece so his answer should be accepted. 这是更重要的部分,因此他的回答应该被接受。

So the whole code block would become: 因此,整个代码块将变为:

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));
}

This may help you https://codepen.io/anon/pen/xQLzXx 这可以帮助您https://codepen.io/anon/pen/xQLzXx

var arr = ['foo','bar'];
var arr2 = ['baz', 'boo']
arr.push(arr2);
console.log(arr);
document.write(arr);
document.write("<br>");
document.write(JSON.stringify(arr));

It's basically just the way it writes it to document. 基本上,这只是将其写入文档的方式。 If you log it in console you'll see the array appended. 如果在控制台中登录,则会看到附加的数组。 Or if you JSON.stringify() first it will show as you expect. 或者,如果首先使用JSON.stringify() ,它将按预期显示。

My advice is ALWAYS console.log() so you can see exactly how the data is structured 我的建议始终console.log()这样您就可以准确了解数据的结构

The others have already pointed out what the problem is (+ there's a typo in one of your variable names - arr_rop vs arr_por ). 其他人已经指出了问题所在(+您的变量名之一arr_roparr_por )。 Here's an ES6 version that will break in older browsers, for learning purposes: 这是一个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));
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM