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?

7
  • 6
    How can I create an object with dash in the key name? - You already have done. Commented Sep 18, 2015 at 17:30
  • 1
    well, arrays are objects. All you have to do is change new Array(); to {} to have an object that isn't an array. Commented Sep 18, 2015 at 17:33
  • 1
    stackoverflow.com/questions/8630471/… Commented Sep 18, 2015 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'} Commented Sep 18, 2015 at 17:33
  • 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. Commented Sep 18, 2015 at 17:45

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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, 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.
@Paulpro - fair enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.