0

I like to create an object using javascript/node.js with dash in the middle of the key's name.

Here is an array that I am using today that need to convert to an object.

var data = new Array();
data['__type'] = 'urn:inin.com:connection:icAuthConnectionRequestSettings';
data['applicationName'] = 'test';
data['userID'] = 'blah';
data['password'] = 'blah;
data['val-name'] = 'blah;

How to create an object with key like this val-name?

Junior
  • 11,602
  • 27
  • 106
  • 212
  • 6
    *How can I create an object with dash in the key name?* - You already have done. – thefourtheye Sep 18 '15 at 17:30
  • @thefourtheye I am not sure what I am missing here, I thought I create an array. – Junior Sep 18 '15 at 17:33
  • 1
    well, arrays are objects. All you have to do is change `new Array();` to `{}` to have an object that isn't an array. – Kevin B Sep 18 '15 at 17:33
  • 1
    http://stackoverflow.com/questions/8630471/strings-as-keys-of-array-in-javascript – Andreas Sep 18 '15 at 17:33
  • 3
    An Array is an Object and you're able to define properties on it. However, if you're not using it for its array purposes, you might as well `data = {}` or `data = {__type: 'urn:...', applicationName: 'test, ..., 'val-name': 'blah'}` – arcyqwerty Sep 18 '15 at 17:33
  • Thank you all for your help. @thefourtheye since you had the first comment, please post an answer and I will accept it. – Junior Sep 18 '15 at 17:40
  • 1
    An answer to the question in the title: Not possible, any key name JS accepts is legal, illegal names are not accepted, no matter what you try. – Teemu Sep 18 '15 at 17:45

1 Answers1

2

I've got a few spare minutes. So, code-review hat goes on.

var data = new Array();
data['__type'] = 'urn:inin.com:connection:icAuthConnectionRequestSettings';
data['applicationName'] = 'test';
data['userID'] = 'blah';
data['password'] = 'blah;
data['val-name'] = 'blah;

Firstly, I think you have some typographic errors in this code. The last two values have oddly paired quotes.

var data = new Array();
data['__type'] = 'urn:inin.com:connection:icAuthConnectionRequestSettings';
data['applicationName'] = 'test';
data['userID'] = 'blah';
data['password'] = 'blah';
data['val-name'] = 'blah';

Next, at the moment, you're assigning keys to an array. Which probably isn't what you mean (summary of the issue here; short version is that some collection methods will give you unexpected results). You likely mean to start an empty object as data.

var data = {};
data['__type'] = 'urn:inin.com:connection:icAuthConnectionRequestSettings';
data['applicationName'] = 'test';
data['userID'] = 'blah';
data['password'] = 'blah';
data['val-name'] = 'blah';

Finally, you can use data literals in JS, rather than serial assignment.

var data = {
    '__type': 'urn:inin.com:connection:icAuthConnectionRequestSettings',
    'applicationName': 'test',
    'userID': 'blah',
    'password': 'blah',
    'val-name': 'blah'
}

As part of this, you've created an object with a slot name that has a - in it. There's nothing illegal about this, but it does prevent you from accessing that slot with dot notation.

console> data['val-name']
'blah'
console> data.val-name
NaN

That has nothing to do with the key being illegal, and everything to do with the access being parsed as a subtraction. That is, data.val-name gets interpreted as "Subtract the value of name from the value of data.val" rather than "Access the slot val-name of the object data".

Inaimathi
  • 13,853
  • 9
  • 49
  • 93
  • 1
    I would change the line `"Which probably isn't going to work"` to something like, "which is probably not what you meant" and make it a link to an article [like this](http://www.laurencegellert.com/2012/01/associative-arrays-in-javascript/), or maybe a better article (I didn't look very hard). The truth is that it probably will work, there is just no reason to use an array which always has length 0, when what you want is an object. – Paul Sep 18 '15 at 17:56
  • @Paulpro - fair enough. – Inaimathi Sep 18 '15 at 17:57