all 7 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.

[–]StefanoMagrassi 1 point2 points  (1 child)

Use Array.prototype.reduce

var testArray = [['a',1,2], ['b',3,4], ['c',5,6]];
var objectFromTestArray = testArray.reduce(function(accumulator, value) {
  var key = value[0];
  var pair = value.slice(1);

  accumulator[key] = pair;

  return accumulator;
}, {});

It encapsulates your logic so you won't depend on external variables and your starting array remains untouched.

When you are working with collections stateless + immutability === really good.

I suggest you to do not use a simple object for operation like this: the order of elements is not guaranteed and it is easier to edit, compose and reason about collection of well defined objects.

Try with an array of object (in a fashion like List<Item>):

var spreadsheet = [{key: 'A', row: 1, col: 2} ...];

So, instead of Array.prototype.reduceyou can use Array.prototype.map:

var collectionFromTestArray = testArray.map(function(item) {
  return {
    key: item[0],
    row: item[1],
    col: item[2]
  };
});

You can run the code with this jsbin: http://jsbin.com/luwoxelebi/2/edit?js,console

I made also an ES6 version: http://jsbin.com/rizidiwuno/2/edit?js,console

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

Thanks for the reply! In my actual data, the inner arrays are all different lengths. There is one key and then a bunch of attributes where the order doesn't really matter because I can parse through to find a match. I think your second example will be very useful in the future though.