在Javascript中拼接和取消后,无法将数组渲染到表中

时间:2022-12-15 21:27:35

I am not sure if I am doing some mistake or is it a default behaviour. I am using splice/unshift to add a new element on top of a 2d array and then try to render that array to a table.

我不确定我是做错了还是默认行为。我正在使用splice / unshift在2d数组的顶部添加一个新元素,然后尝试将该数组渲染到表中。

  for (var i = 0; i < arr1[i].length; i++) {
      for (var j = 0; j < arr1.length; j++) {
          var r = arr1[j][i];
          dt(r);
      }
      dttop(i);
  }

  function dt(x) {
      if (isFinite(x) == true) {
          numeric++;
      } else {
          str++;
      }
  }

  function dttop(x) {
      if (numeric > str)
          arr1.splice(0, 0, "number");
      else
          arr1.splice(0, 0, "string");
  }
  alert(arr1); // alert--1  
   // render the result into html table
  s = '<table border=0>';
  for (var i = 0; i < arr1.length; i++) {
      s = s + '<tr>';
      for (var j = 0; j < arr1[i].length; j++) {
          s = s + '<td><font size="1" face="Verdana">' + arr1[i][j] + '</font></td>';
      }
      s = s + '</tr>';
  }
  s = s + '</table>';
  renderArea.innerHTML = s;

So, when I use javascript splice() or unshift(). I get an output like this在Javascript中拼接和取消后,无法将数组渲染到表中 But, if I do alert 1 then it gives me result like this 在Javascript中拼接和取消后,无法将数组渲染到表中 which proves that it is adding the extra bits from splice function but not rendering as html..

所以,当我使用javascript splice()或unshift()时。我得到这样的输出但是,如果我做警报1然后它给我这样的结果证明它是从拼接功能添加额外的位但不渲染为html ..

Please help

请帮忙

2 个解决方案

#1


1  

From what I can see you are using .splice() to add the string "number" or "string" to the start of the arr1 array. Doing this means that as you loop through to add the values the code tries to access an index of the strings (arr1[i][j]), resulting in undefined.

从我可以看到你使用.splice()将字符串“number”或“string”添加到arr1数组的开头。这样做意味着当您循环以添加值时,代码会尝试访问字符串的索引(arr1 [i] [j]),从而导致未定义。

#2


0  

I am using splice/unshift to add a new element on top of a 2d array and then try to render that array to a table.

我正在使用splice / unshift在2d数组的顶部添加一个新元素,然后尝试将该数组渲染到表中。

The splice() method returns an array containing the elements that were removed from the original array. If you want to prepend values given a two dimensional array:

splice()方法返回一个数组,其中包含从原始数组中删除的元素。如果要在给定二维数组的前提下添加值:

var num_arr = [[1,2,3]];

A simpler solution would be:

一个更简单的解决方案是:

json_arr = JSON.stringify(num_arr).replace("[[","[0,[");

And rendering to a table would be:

渲染到表格将是:

function rowMaster(value,index)
  {
  return "<tr><td>value</td></tr>".replace("value",value);
  }

function parseFarce(value,index)
  {
  if(typeof value == "number") return "<th>value</th>".replace("value",value);

  else if(delete value.length === false && delete value[-1] === true && JSON.stringify(([]).constructor().valueOf()) === "[]") 
    {
    return value.map(rowMaster);
    }

  else
    {
    return value;
    }
  }

JSON.parse(json_arr).map(parseFarce);

References

参考

#1


1  

From what I can see you are using .splice() to add the string "number" or "string" to the start of the arr1 array. Doing this means that as you loop through to add the values the code tries to access an index of the strings (arr1[i][j]), resulting in undefined.

从我可以看到你使用.splice()将字符串“number”或“string”添加到arr1数组的开头。这样做意味着当您循环以添加值时,代码会尝试访问字符串的索引(arr1 [i] [j]),从而导致未定义。

#2


0  

I am using splice/unshift to add a new element on top of a 2d array and then try to render that array to a table.

我正在使用splice / unshift在2d数组的顶部添加一个新元素,然后尝试将该数组渲染到表中。

The splice() method returns an array containing the elements that were removed from the original array. If you want to prepend values given a two dimensional array:

splice()方法返回一个数组,其中包含从原始数组中删除的元素。如果要在给定二维数组的前提下添加值:

var num_arr = [[1,2,3]];

A simpler solution would be:

一个更简单的解决方案是:

json_arr = JSON.stringify(num_arr).replace("[[","[0,[");

And rendering to a table would be:

渲染到表格将是:

function rowMaster(value,index)
  {
  return "<tr><td>value</td></tr>".replace("value",value);
  }

function parseFarce(value,index)
  {
  if(typeof value == "number") return "<th>value</th>".replace("value",value);

  else if(delete value.length === false && delete value[-1] === true && JSON.stringify(([]).constructor().valueOf()) === "[]") 
    {
    return value.map(rowMaster);
    }

  else
    {
    return value;
    }
  }

JSON.parse(json_arr).map(parseFarce);

References

参考