html form values to a javascript array - javascript

how to insert values in a html table, an array multidimensional in javascript.
Additionally, such as performing calculations between the columns of the array and insert the results into a column of the same array.
thanks for your answers.

Javascript code:
var multiArray;
window.onload = function() {
// load the table into the multidimensional array.
multiArray = [];
var trs = document.getElementsByTagName('TR');
for(var i = 0; i < trs.length; i++) {
var arr = [];
var tds = trs[i].childNodes;
for(var j = 0; j < tds.length; j++) {
var td = tds[j];
if (td.tagName === 'TD') {
arr.push(td.innerHTML);
}
}
multiArray.push(arr);
}
// perform some calculations between the columns of the array
var resCalc = [];
for(i = 0; i < multiArray.length; i++) {
resCalc[i] = 0;
for(j = 0; j < multiArray[i].length; j++) {
resCalc[i] += multiArray[i][j];
}
}
// insert the results into a column of the same array
var columnToModify = 0; // the index of the column you want to change
for(i = 0; i < multiArray.length; i++) {
multiArray[i][columnToModify] = resCalc[i];
}
};

something on Google…
http://www.eggheadcafe.com/community/aspnet/3/10003047/html-table-in-to-an-array.aspx

Related

How to iterate through this html table and get data from rows whose last col is not empty?

I've been trying to get this data, but the array comes out empty:
function saveData() {
var tableData = [];
var table = document.getElementById("dtable");
console.log(table)
for (var i = 0; i < table.length; i++) {
let tableCells = table.rows.item(i).cells;
let cellLength = tableCells.length;
for (let j = 0; j < cellLength; j++) {
let cellVal = tableCells.item(j).innerHTML;
tableData.push(cellVal)
}
}
console.log(tableData)
rows = JSON.stringify(tableData);
google.script.run.formToSheets(rows);
}
Thanks for any help!
In your script, as one of several possible modifications, how about the following modification?
From:
var tableData = [];
var table = document.getElementById("dtable");
console.log(table)
for (var i = 0; i < table.length; i++) {
let tableCells = table.rows.item(i).cells;
let cellLength = tableCells.length;
for (let j = 0; j < cellLength; j++) {
let cellVal = tableCells.item(j).innerHTML;
tableData.push(cellVal)
}
}
console.log(tableData)
To:
var table = document.getElementById("dtable");
console.log(table)
var [, ...tr] = table.querySelectorAll("tr");
var tableData = [...tr].map(r => {
var td = r.querySelectorAll("td");
return [...td].map((c, j) => j != 3 ? c.innerHTML : c.querySelectorAll("select")[0].value);
});
console.log(tableData)
In this modification, from a table, the table rows are retrieved. And, the table cells are retrieved from the table rows. In teh case of the dropdown list, the value is retrieved from the dropdown list.
Note:
When this modification was not the direct solution to your issue, I think that when you provide your whole script, it will help to correctly understand your question.

Remove table if contains string

I am looking to automatically remove tables in slides if they contain a specific text string. This is what I currently have, but for some reason, the findText() doesn't seem to work and I can't figure out an alternative to this...
function removeUnwantedTables() {
var gotSlides =
SlidesApp.openById('1gJjGBbaQXWhP8uhVIoccV2h_RL7_gsxvg_NW-qNCcLU').getSlides();
for (var i = 0; i < gotSlides.length; i++) {
var slide = gotSlides[i];
var tables = slide.getTables();
for (var k = 0; k < tables.length; k++) {
var allTables = tables[k];
if (allTables.findText('{{remove-this-table}}') > 0) {
allTables.remove();
}
}
}
}
Does anyone have a solution to this?
How about this modification? I think that there may be several answers. So please think of this as one of them.
Modification points :
Using getCell(), each cell are retrieved and compared to the string of {{remove-this-table}}.
I couldn't find the method for directly searching the string from a table. So I used this.
When {{remove-this-table}} is found, the table is removed and the for loop is broken away.
Modified script :
function removeUnwantedTables() {
var gotSlides = SlidesApp.openById('1gJjGBbaQXWhP8uhVIoccV2h_RL7_gsxvg_NW-qNCcLU').getSlides();
for (var i = 0; i < gotSlides.length; i++) {
var slide = gotSlides[i];
var tables = slide.getTables();
if (tables.length > 0) {
for (var k = 0; k < tables.length; k++) {
var allTables = tables[k];
row = allTables.getNumRows();
col = allTables.getNumColumns();
var values = [];
for (var r = 0; r < row; r++) {
for (var c = 0; c < col; c++) {
var v = allTables.getCell(r, c).getText().asString();
if (v.indexOf("{{remove-this-table}}") > -1) {
values.push(v);
break;
}
}
if (values.length > 0) {
allTables.remove();
break;
}
}
}
}
}
}
If this was not what you want, I'm sorry.

How to I run a function for each piece of an array within a for loop in Javascript?

I have this so far, trying to get it to find the sum of each one of any number of inputted numbers with integers and "-"s.
When I run this,
var howM = prompt("How many cards?")
var arr = [];
for (var i = 0; i < howM; i++)
arr.push(prompt("Enter a card:"));
console.log(arr)
var sumpre = [];
for (var i = 0; i <= howM; i++) {
var sum = 0;
var eXt = arr[i];
eXt = eXt.replace(/-/g, "");
for (i = 0; i < eXt.length; i++) {
sum += parseInt(eXt.substr(i, 1));
}
sumpre.push(sum);
}
console.log(sumpre)
I have also tried
var howM = prompt("How many cards?")
var arr = [];
for (var i = 0; i < howM; i++)
arr.push(prompt("Enter a card:"));
console.log(arr)
for (var i = 0; i < howM; i++) {
var sum = 0;
var eXt = arr[i]
eXt = eXt.replace(/-/g, "");
for (i = 0; i < eXt.length; i++) {
sum += parseInt(eXt.substr(i, 1));
}
}
console.log(sum);
In both cases I get the sum for the first piece in the array and then undefined. How do I get it to run for each piece? I kind of have an idea of what is wrong with it I just don't quite know how to fix it.
You need to use a second counter for the nested for loop, like so:
var howM = prompt("How many cards?")
var arr = [];
for(var i = 0; i < howM; i++)
arr.push(prompt("Enter a card:"));
console.log(arr)
var sumpre = [];
for(var i = 0; i < howM; i++) {
var sum = 0;
var eXt = arr[i];
eXt = eXt.replace (/-/g, "");
for (var j = 0; j < eXt.length; j++) {
sum += parseInt(eXt.substr(j, 1));
}
sumpre.push(sum);
}
console.log(sumpre)
Your var sum = 0; inside your for-loop meaning sum variable will not be accessible outside of the loop

How to get id from an innerHTML in javascript?

I have written the following function.
There are controls inside Table which is inside a div.
I need to print id of all the controls
function getID() {
debugger;
var ids = [];
var children = document.getElementById("divAlarmSection").children;
for (var i = 0, len = children.length; i < len; i++) {
ids.push(children[i].id);
var table = document.getElementById(children[i].id);
var rows = table.rows;
for (i = 0, n = rows.length; i < n; ++i) {
var cells = rows[i].getElementsByTagName('td');
for (var x = 0; x < cells.length; x++) {
if (cells[x].length != 0) {
alert(cells[x].innerHTML);
}
}
}
}
use .getElementsByTagName("*") on cells[x] and access 'ID' property of the result. So your last 'for' loop becomes:
var elements = cells[x].getElementsByTagName("*")
for(var len=0;len<elements.length;len++)
console.log(elements[len].id);
Also, if you know the inner structure of each 's and its consistent, you can look for that element in .getElementsByTagName()

Create multidimensional array in for loop

I want to create a multidimensional array like this:
array[0][1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
array[1][21,22,23,24,25,26,27....]
array[.][....]
How can I do this in Javascript?
I have tried this:
var squares = new Array();
for(var i = 1; i <= 8; i++)
{
for(var j = 1; j <= 20; j++)
{
squares.push(i, j);
}
}
How can I accomplish this?
You can do something like this:
var squares = new Array();
for(var i = 0; i <= 8; i++)
{
squares[i] = new Array();
for(var j = (i * 20) + 1; j <= 20 * i + 20; j++)
if (squares[i] == null)
squares[i] = j;
else
squares[i].push(j);
}
Output comes like:
array[0][1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
array[1][21,22,23,24,25,26,27....]
var array = []; // Main array
var numArrays = 10, // Number of sub-arrays
numPerArray = 20; // Number of squares per sub-array
for(var i = 0; i < numArrays; i++){
var subArray = [];
// Number to start at
var start = i * numPerArray;
// Count up to start + numPerArray
for(var j = start; j < start + numPerArray; j++){
subArray.push(j);
}
// Add to main array
array.push(subArray);
}
Use modulus operand to limit the inner array's size
var limit = 80
var inner_limit = 20
var square=[]
var inner =[]
for(var i=1;i<=limit;i++){
inner.push(i)
if(i%inner_limit==0){
square.push(inner)
inner = []
}
}
You can do it with two "for" loops. In the first loop you go through the main array and for each element add the elements from the second loop.
var arrayLength = 10; // Main array length
var limit = 20; // Number of squares
var array = [];
for ( var i = 0; i < arrayLength; i++ )
{
array[i] = []; // Create subArray
for( var j = 1; j <= limit; j++ )
{
array[i].push(j);
}
}

Categories

Resources