3 Dimensional Arrays (Creation / Manipulation)

Hi All:

Totally new to OmniGraffle Automation and doing things brute force via “copy as javascript” and Automation Console to try learn the internal constructs/manipulations. I need to create some sort of construct to deal with 3 space. I think a 3 dimensional array will provide what I need but I’m stumbling over the mechanics.

Starting with a 2-dimensional array:

var myArr = new Array();
myArr[0] = new Array();
myArr[0] = “abcde”;
myArr[1] = “fghij”;
myArr[2] = “klmno”;
myArr[0][0] = “pqrst”;
myArr[0][1] = “uvwxy”;

g1.text = "xval1: " + myArr[0] + ", yval1: " + myArr[0][0] + ", xval2: " + myArr[1] + ", xval3: " + myArr[2];

For myArr[0][0] I’m expecting to see “pqrst” as the result.
Thinking it would be the first element in both the 1st and 2nd dimensions of a 2 dimensional array

What I’m seeing is:

myArr[0] : “abcde”
myArr[1] ": “fghij”
myArr[2] : “klmno”

myArr[0][0] is the 1st byte of 1st element of the 1st dimension (i.e. “a”)
myArr[0][1] is the 2nd byte of 1st element of the 1st dimension (i.e. “b”)
myArr[0][2] is the 3rd byte of 1st element of the 1st dimension (i.e. “c”)
myArr[0][3] is the 4th byte of 1st element of the 1st dimension (i.e. “d”)
myArr[0][4] is the 5th byte of 1st element of the 1st dimension (i.e. “e”)
myArr[0][5] is the 6th byte of 1st element of the 1st dimension (i.e. doesn’t exist)

Thought I would see:

myArr[0][0] => “pqrst”
myArr[0][1] => “uvwxy”

It’s clear that it’s looking at each of the bytes in the 1st dimension one by one with that second [x] index rather than the 1st element in the 1st dimension and the 1st element of its associated 2nd dimension for [0][0] as an example.

Any thoughts on what I’m doing wrong here?

Thanks.

you set the array at 0 first as an array, then again as a string. Strings are array-like in JS.

try this:
var a = “abc”
console.log(a[1])

see this playcode entry

Are you looking for a way to represent coordinates in 3 dimensional space? Here’s one way to do that.

``````// constructor for point
function Point(x, y, z) {
this.x = x
this.y = y
this.z = z
}
// one point
var p = new Dot(3, 4,2 )
// access dimensions
console.log(`x: \${p.x} y: \${p.y} z:{p.z}`)
``````

Of course, you can collect some points in an array:

``````// an array of 3 points
var pts = [new Point(1, 2, 3), new Point(4, 5, 6), new Point(7, 8, 9)]
// access dimensions of the section point
console.log(pts[2].x, pts[2].y, pts[2].z)
``````

see this playcode entry

Thank you bboc. Bouncing through all of your input as well as juggling through other aspects of what I’m hoping to accomplish. Not moving fast right now but chewing on all of it. One byte at a time. Thank you!!!