orkun Posted January 4, 2011 Share Posted January 4, 2011 helloI have a string in this structure:[ [ [ 32.090000, 41.110000 ], [ 32.090537, 41.110510 ], [ 32.091100, 41.110991 ], [ 32.100000, 41.114142 ] ] ]I want to put it in an array (or any other possible structure) to get a value in this form: [ 32.090537, 41.110510 ]can I do that?I would be appreciate if you supply an answer.regardsAhmet TEMİZ Link to comment Share on other sites More sharing options...
jeffman Posted January 4, 2011 Share Posted January 4, 2011 If I understand you, the following is a string of character data that could be written in quotes? var str = "[ [ [ 32.090000, 41.110000 ], [ 32.090537, 41.110510 ], [ 32.091100, 41.110991 ], [ 32.100000, 41.114142 ] ] ]"; If that is correct, turn it into an array object like so: var obj = eval(str);var myArray = obj[0][1]; // this will return [ 32.090537, 41.110510 ] as a simple array. Notice that the second and third set of brackets makes this a multi-dimensional array, which is why we need multiple indexes to access the correct value. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 4, 2011 Share Posted January 4, 2011 Are you trying to return [ 32.090537, 41.110510 ] as a string or an array? The way DD showed you will return it as an array with two elements. If you want that as a string, you might have to write your own parsing function. Or a function to convert the array to a string in that format, ie a function that returns something like this:return "["+myArray[0]+", "+myArray[1]+"]"; Link to comment Share on other sites More sharing options...
orkun Posted January 4, 2011 Author Share Posted January 4, 2011 thank youPlease let me extend the question.what if [ [ [ 32.090000, 41.110000 ], [ 32.090537, 41.110510 ], [ 32.091100, 41.110991 ], [ 32.100000, 41.114142 ] ] ] is actuallyan array, and I want to get [ 32.090000, 41.110000 ] as an index of this array. I mean array[0]=[ 32.090000, 41.110000 ]what do I have to do ?regards Link to comment Share on other sites More sharing options...
thescientist Posted January 4, 2011 Share Posted January 4, 2011 thank youPlease let me extend the question.what if [ [ [ 32.090000, 41.110000 ], [ 32.090537, 41.110510 ], [ 32.091100, 41.110991 ], [ 32.100000, 41.114142 ] ] ] is actuallyan array, and I want to get [ 32.090000, 41.110000 ] as an index of this array. I mean array[0]=[ 32.090000, 41.110000 ]what do I have to do ?regardsif you don't need a multi-dimensional array, then just create an array like normal, with each index being a string. i.e.var array = new Array();array[0] = "32.090000, 41.110000";array[1] = "32.090537, 41.110510";array[2] = "32.091100, 41.110991";array[3] = "32.100000, 41.114142";for (i=0;i<array.length;i++){ document.write(array[i] + "<br />");} outputs32.090000, 41.11000032.090537, 41.110510 32.091100, 41.11099132.100000, 41.114142edit: or in your case, since you are eval'ing a string, ["32.090000, 41.110000"], ["32.090537, 41.110510"], ["32.091100, 41.110991"], ["32.100000, 41.114142"] Link to comment Share on other sites More sharing options...
justsomeguy Posted January 4, 2011 Share Posted January 4, 2011 var ar = [ [ [ 32.090000, 41.110000 ], [ 32.090537, 41.110510 ], [ 32.091100, 41.110991 ], [ 32.100000, 41.114142 ] ] ];for (var i = 0; i < ar.length; i++){ for (var j = 0; j < ar[i].length; j++) { for (var k = 0; k < ar[i][j].length; k++) { document.write('array[' + i + '][' + j + '][' + k + '] = ' + ar[i][j][k] + '<br>'; } }} That's going to print entries like array[0][0][0], array[0][0][1], array[0][1][0], etc. The base array is an array with one element, and that one element is another array that has 4 elements, and each of those elements is an array with 2 elements. I'm not sure why you need an array of 4 coordinates nested that deep, but that's the way you have it shown. The number 41.110991, for example, is located at ar[0][2][1]. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.