-1

I need to end up with a parent array that has several child arrays to be able to send it in through an ajax post.

My html:

<tr class="row"> <td><input type="text" /></td> <td><input type="date" /></td> <td><input type="number /></td> <td><input type="number /></td> </tr> <tr class="row"> <td><input type="text" /></td> <td><input type="date" /></td> <td><input type="number /></td> <td><input type="number /></td> </tr> 

My javascript:

var detail = new Array() $("tr.line").each(function(){ var row = new Array(); $("tr.line input").each(function(){ row.push(this.value); }); detail.push(row); }); 

But this isn't doing what I need it to do

detail = array( row = array( text: ..., date: ..., number: ..., number: ... ) row = array( text: ..., date: ..., number: ..., number: ... ) ) 
6
  • Can you provide a sample input of what you would like it to look like? I don't see anything wrong with this per se, other than the duplicating keys inside of detail. Commented Jan 22, 2017 at 3:23
  • Ohgodwhy, the sample is just above. It's the last code block in my question. Commented Jan 22, 2017 at 3:25
  • How do you want the json object look like? Commented Jan 22, 2017 at 3:29
  • "tr.line" - well, you don't have any tr with class line - so you wont get much with your code - perhaps you meant "tr.row" Commented Jan 22, 2017 at 3:39
  • what I need it to do - you need the rows array to be an array of objects, not an array of arrays Commented Jan 22, 2017 at 3:41

1 Answer 1

2

Your result row = Array(text: ...) better fits the key: value pairs of an object.
html:

<tr class="line"> <td><input name="text1" type="text" /></td> <td><input name="date1" type="date" /></td> <td><input name="number1" type="number /></td> <td><input name="number2" type="number /></td> </tr> 

js:

var detail = []; $("tr.line").each(function(){ var row = {}; $(this).find('input').each(function(){ row[this.name] = this.value; }); detail.push(row); }); 

result:

detail = [ { text1: ..., date1: ..., number1: ..., number2: ... } ] 
Sign up to request clarification or add additional context in comments.

1 Comment

This is it!! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.