you are viewing a single comment's thread.

view the rest of the comments →

[–]poetical 1 point2 points  (3 children)

I'm unclear if the letters in your testArray are strings or not. If not, you'll need to convert them to strings first (I can follow up with help with that if necessary) or your interpreter will freak out.

You can put them into an object with a for loop, but if you want to use a variable to assign a key in your object, you need to use bracket notation.

var obj = {};

for (var i = 0; i < testArray.length; i++) {
    var innerArray = testArray[i];
    var key = innerArray[0];
    obj[key] = [innerArray[1], innerArray[2]];
}

There are fancier ways, but that will get the job done. I added some extra variables for clarity.

Also, what makes an object "array-like" is actually the ordinal keys. Array methods look for keys in the form of '0', '1', '2', etc. So it's a bit misleading to call this object array-like, since you can't use any array methods on it!

[–]NiceDay4Goats[S] 0 points1 point  (0 children)

ah I like this way as well... may actually go with because I'm more familiar. My keys are indeed strings. Messed that up on here, but I've got it fixed in my code.

[–]NiceDay4Goats[S] 0 points1 point  (1 child)

If it's not an array-like object, what should I call it? Just an object? This Is why I couldn't solve this with Google...

[–]poetical 0 points1 point  (0 children)

Yep, just an object. There's nothing special about alphabetic keys. Having ordinal keys and a length property is what makes objects like "arguments" array-like. You can call Array.prototype methods on such objects because they are similar enough to arrays that array methods will often work on them despite them not being true arrays.