Jump to content



Photo
jquery ajax array of strings post web services javascript

  • Please log in to reply
21 replies to this topic

#16 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 21 February 2013 - 22:13

Its an array initialization problem... once I sized it correctly when dimensioning the array variable it worked fine:

var arrayToStore = [['','',''],['','',''],['','','']]

I did not think that Javascript arrays had to be strictly dimensioned... Is there a short hand for this? I would think new Array(3,3) or something would look better...


I had worked that code up in a fiddle and was getting the same error, hence why I posted it with ['','']. The general consensus seems to be to create a helper fuction that, given a number x, can push x number of blank objects into the array which you can then fill.

Or... just use an object. It's certainly the preferred way in JS unless you really need an array of arrays.

#
# CORRECT WAY TO INSTANTIATE AN ARRAY WITHOUT DIMENSION
#

var arrayToStore = [];

#
# PROBLEM
#

var arrayToStore = [['',''],['','']];

#
# REASON
#

IF you instantiate the array with only the above "problem" dimensions and do not correct the length beyond this, they will be truncated.


If you do arrayToStore = [] and then try to set arrayToStore[0][0] = something, you'll get the same error.


#17 tim_s

tim_s

    Default

  • Joined: 07-January 13
  • OS: OSX (Macbook Pro i7), Windows 7 (Gaming), Gentoo
  • Phone: Samsung Galaxy SIII, iPhone 4s

Posted 21 February 2013 - 22:36

If you gave me an example of how you would like the content it might make it easier but off the top of my mind - how does this work?

var arrayData = [];

function ProcessButtonClick() {

    var $strings = $('form input');
 
    $strings.each(function(){
    
        var arrayParent = [];
        var arrayChild = [];
 
	    var x = parseInt($(this).attr('data-row'));
	    var y = parseInt($(this).attr('data-col'));

        arrayChild[x] = $(this).val();
        arrayParent[y].push(arrayChild);
        arrayData.push(arrayParent)
 
    });

 
 
}


#18 OP Shadrack

Shadrack

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 20-December 01

Posted 21 February 2013 - 23:27

tim_s - I get this error:

TypeError: arrayParent[y] is undefined

arrayParent[y].push(arrayChild);


Seems like the only way for me to get it to work is to declare the full size of the array... in just about every language you can initialize the size of an array directly but for some reason this doesn't seem possible in Javascript unless I'm missing something.

In the end, the size of my table is fixed so dimensioning out the variable before hand is fine.

#19 tim_s

tim_s

    Default

  • Joined: 07-January 13
  • OS: OSX (Macbook Pro i7), Windows 7 (Gaming), Gentoo
  • Phone: Samsung Galaxy SIII, iPhone 4s

Posted 21 February 2013 - 23:46

What is the value of y?

#20 OP Shadrack

Shadrack

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 20-December 01

Posted 22 February 2013 - 03:45

What is the value of y?


It's 0 before it calls that line and throws the error.

#21 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 22 February 2013 - 12:40

Seems like the only way for me to get it to work is to declare the full size of the array... in just about every language you can initialize the size of an array directly but for some reason this doesn't seem possible in Javascript unless I'm missing something.

In the end, the size of my table is fixed so dimensioning out the variable before hand is fine.


You're not, as I mentioned, it's not a preferred way of doing things in JS. If you must, this will work:
<script>
var multidArray = function(y){
var array = [];
for(i=0;i<y;i++){
  array.push(['example', 'array']);
}
return array;
}
var newArray = multidArray(6);
newArray[1][0] = ['now', 1,1];
newArray[1][1] = 'modified';
console.log(newArray);
</script>

However, in JS, this is preferable:
var better = {};
better[0] = {1: 'this', 2 : 'will', 3: 'work', 4: 'much', 5: 'better'};
console.log(better[0][1]); // will log 'this'


#22 OP Shadrack

Shadrack

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 20-December 01

Posted 22 February 2013 - 15:19

Well..its all moot because I can't get the input array into my web service.... apparently arrays of strings are a supported output (in JSON format) but not a supported input. I'll have to turn my table into CSV and then program my web service accordingly.