0

I've seen the big answer already, and everyone gives completely different answers with varying degrees of complexity.

I'm trying to do this:

var tempFiles=[];
tempFiles[req.query.tenant,file.name]=finalName;

I'm not sure if this is working or not.

When I console.log(tempFiles), I get

[ 'the value for file.name ': 'the value for final name' ]

Where did the value for req.query.tenant go? Is this a proper 2D array?

Community
  • 1
  • 1

1 Answers1

6

You can't use commas for this. You have to have separate [ ] operators.

tempFiles[req.query.tenant][file.name]=finalName;

Perversely, your code was not a syntax error because the comma operator does exist. The meaning of your version was:

  • evaluate the expression req.query.tenant
  • throw that value away
  • evaluate the expression file.name
  • use that value as a property name to look up in the object referenced by "tempFiles"

Also, note that if you really just declared your array right before trying to make that assignment, it won't work. You have to explicitly create the second dimension:

var tempFiles = [];
tempFiles[ req.query.tenant ] = [];
tempFiles[ req.query.tenant ] [ file.name ] = finalName;

Finally, if the property names involved — req.query.tenant and file.name — are strings, then you really shouldn't be using arrays anyway. You should be creating plain objects:

var tempFiles = {};
tempFiles[ req.query.tenant ] = {};
tempFiles[ req.query.tenant ] [ file.name ] = finalName;
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • That gives me an error `Cannot set property '[value for final name]' of undefined` –  Jan 24 '14 at 01:06