0

I'm trying to push values into specific elements by creating a multidimensional array:

var rate_info = [];
rate_info[0].push($("#title-textarea"+id).val());
rate_info[0].push($("select[name=category"+id+"]").val());
rate_info[0].push($("select[name=subcategory"+id+"]").val());
...
data: {
    "rate_info" : rate_info,
},
...

but this breaks my script and I'm not sure what I'm doing wrong

2
  • 1
    Please explain the part of "breaks my script". What is the error? where does it fail? Commented Dec 5, 2012 at 9:35
  • when I try without creating multidimensional array (rate_info.push instead of rate_info[0]) it works. But I want to push the values into rate_info at position [0] since I got mroe values coming after it in the next iteration to go into rate_info[1] 2 3 etc. Commented Dec 5, 2012 at 9:37

2 Answers 2

4

No need in indexing, just push to the array:

rate_info.push($("#title-textarea"+id).val());
rate_info.push($("select[name=category"+id+"]").val());
rate_info.push($("select[name=subcategory"+id+"]").val());

Otherwise you need to create a new array first to be able to push elements to it:

var rate_info = [ [] ];
rate_info[0].push($("#title-textarea"+id).val());
Sign up to request clarification or add additional context in comments.

3 Comments

The OP mentioned multi dimensional array. I guess OP is using an array of arrays here, and thats why the indexing.
+1 And if you want to make it multidimensional rate_info.push(some_array)
Okay I go with this solution create arrays each iteration and push into rate_info array. Thanks!
2

As you're using a multidimensional array you need to make sure that rate_info[0] is an array so it's possible to push to it, i.e.

rate_info[0] = [];

Before attempting to do the push calls. At the moment you're attempting to push to undefined.

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.