0

i have a table in which dynamic rows are coming, so i don't know how many rows. Inside a td in the rows, there is some content that needs to be registered in an array on click of a button.

i need to create an array with that content, but it should be in this format:

array1 = [ row1:"", row2:"abc", row3:'', row4:'', row5:'xyz' ]

here, the row1...row5 comes dynamically based on the number of rows. By default the value of the rows can be '', thats fine.

Thanks in advance.

3
  • arrays can only have 0... N (numeric) indexes Commented May 11, 2018 at 8:01
  • 1
    @Eddie not really, just array literals can not have properties. Commented May 11, 2018 at 8:01
  • @NinaScholz Ohh Nice to know :) Commented May 11, 2018 at 8:02

2 Answers 2

1
array1 = [ row1:"", row2:"abc", row3:'', row4:'', row5:'xyz' ]

The above will not work as it it is incorrect syntax. What you can do is create an array of objects and use that instead.

array1 = [{row1:""},{row2:"abc"}];

Or, if the row number AND value are important, this may be a better structure:

array1 = [{rowId:1, value:""}, {rowId:2, value:"abc"}];

EDIT:

To create such a structure from an existing HTML Table, you can query for the rows and operate on each row to create the array.

// Get a reference to the table element
var table = document.getElementById("table1");
// Initialise empty array
var array = [];

// Query for all the tr elements of the table (rows)
table.querySelectorAll('tr').forEach(function(row, index){
    // For each row, extract value from the requried column
    var value = row.children[0].innerText; // Assuming the value to be added is in the first column (td)
    // Insert new object into array for this row
    array.push({rowId:index, value: value});
})

console.log(array); // array will be an array of our objects
Sign up to request clarification or add additional context in comments.

4 Comments

yes, but how to make this dynamically baed on number of rows?
You can easily make this dynamically. But to answer accurately, I will need to understand your data source. From where are you creating this array? From HTML? from API?
@user3450590 I have updated my answer with a demo of creating it from a html table
agree with @Chirag Ravindra updated answer should be the choice.
0

with a little modification in @Chirag Ravindra code

  // Get a reference to the table element
var table = document.getElementById("table1");
// Initialise empty array
var array = [];

// Query for all the tr elements of the table (rows)
    table.querySelectorAll('tr').forEach(function(row, index){
// For each row, extract value from the requried column
var value = row.children[0].innerText; // Assuming the value to be added is in the first column (td)
// Insert new object into array for this row
array["row" + index] = value;
})

console.log(array); // array will be an array of our objects

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.