Skip to content Skip to sidebar Skip to footer

Unable To Render An Array Into Table After Splice And Unshift In Javascript

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 ta

Solution 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.


Solution 2:

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.

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:

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


Post a Comment for "Unable To Render An Array Into Table After Splice And Unshift In Javascript"