0

I have a table with phone numbers and Forms and trying to add them all to 1 data object eg.


name: name
ph: [phone numbers]
Forms:[abForm]

but it is only giving me the last phone number and the last abForm of the table in the object.

// loop after 1st row    
for (r = 1; r < rowcount; r++) {
  cells = rows[r].cells;
  // first column phonenumbers    
  cellcount = 1;
  for (c = 0; c < cellcount; c++) {
    tel = cells[c].innerText;
    cellcount2 = 2;
  }
  // second column Forms
  for (c = 0; c < cellcount2; c++) {
    abForm = cells[c].innerText;
  }
}
let phone = [tel];
var data = {
  name: 'name',
  ph: [phone],
  Form: [abForm]
}
console.log(data)
// write out      
}
2
  • You surely have your variables defined before the loop, right? Commented Mar 27, 2020 at 10:28
  • You also reassign the values each loop so only the last one is in that variable. Commented Mar 27, 2020 at 10:30

1 Answer 1

1

Since your example is not working code, I will show you the key steps.

Declare your variable first

Declare two variables of type array

const tel = [],
  abForm = []

add to the array

use the in built array function push to add new values to the array

tel.push(cells[c].innerText);

assign the result

since the variables' are already arrays, we can simply assign the whole array to one key of the data object.

var data = {
  name: 'name',
  ph: phone,
  Form: abForm
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.