Having trouble in my first time trying to use switch case [duplicate] - javascript

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
I am trying to do this:
var KEYS = {} ;
KEYS.PHONE_TYPE = 'phone-type';
KEYS.AGENT_TYPE = 'agent-type';
var myAppConfig = {
...
iconMap : {
KEYS.PHONE_TYPE : 'icon-phone',
KEYS.AGENT_TYPE : 'icon-headphones'
};
...
};
But it is failing, with a message: Expected ':' and instead saw '.'.
How can I initialize an object using indirect (non-literal) keynames?
To be clear, the result I want is:
{
'phone-type' : 'icon-phone',
'agent-type' : 'icon-headphones'
}

If you're using ES6 (or something like Babel/browserify), you can write it like this:
iconMap : {
[KEYS.PHONE_TYPE] : 'icon-phone',
[KEYS.AGENT_TYPE] : 'icon-headphones'
};

You would have to add those properties separately using bracket notation:
var myAppConfig = {
...
iconMap : { }
...
};
myAppConfig.iconMap[ KEYS.PHONE_TYPE ] = 'icon-phone';
myAppConfig.iconMap[ KEYS.AGENT_TYPE ] = 'icon-headphones';

Related

How do I create a object set with a key? [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
I am trying to do this:
var KEYS = {} ;
KEYS.PHONE_TYPE = 'phone-type';
KEYS.AGENT_TYPE = 'agent-type';
var myAppConfig = {
...
iconMap : {
KEYS.PHONE_TYPE : 'icon-phone',
KEYS.AGENT_TYPE : 'icon-headphones'
};
...
};
But it is failing, with a message: Expected ':' and instead saw '.'.
How can I initialize an object using indirect (non-literal) keynames?
To be clear, the result I want is:
{
'phone-type' : 'icon-phone',
'agent-type' : 'icon-headphones'
}
If you're using ES6 (or something like Babel/browserify), you can write it like this:
iconMap : {
[KEYS.PHONE_TYPE] : 'icon-phone',
[KEYS.AGENT_TYPE] : 'icon-headphones'
};
You would have to add those properties separately using bracket notation:
var myAppConfig = {
...
iconMap : { }
...
};
myAppConfig.iconMap[ KEYS.PHONE_TYPE ] = 'icon-phone';
myAppConfig.iconMap[ KEYS.AGENT_TYPE ] = 'icon-headphones';

Javascript function to create nested array/object with variable input [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
I am trying to do this:
var KEYS = {} ;
KEYS.PHONE_TYPE = 'phone-type';
KEYS.AGENT_TYPE = 'agent-type';
var myAppConfig = {
...
iconMap : {
KEYS.PHONE_TYPE : 'icon-phone',
KEYS.AGENT_TYPE : 'icon-headphones'
};
...
};
But it is failing, with a message: Expected ':' and instead saw '.'.
How can I initialize an object using indirect (non-literal) keynames?
To be clear, the result I want is:
{
'phone-type' : 'icon-phone',
'agent-type' : 'icon-headphones'
}
If you're using ES6 (or something like Babel/browserify), you can write it like this:
iconMap : {
[KEYS.PHONE_TYPE] : 'icon-phone',
[KEYS.AGENT_TYPE] : 'icon-headphones'
};
You would have to add those properties separately using bracket notation:
var myAppConfig = {
...
iconMap : { }
...
};
myAppConfig.iconMap[ KEYS.PHONE_TYPE ] = 'icon-phone';
myAppConfig.iconMap[ KEYS.AGENT_TYPE ] = 'icon-headphones';

JavaScript create an object like {Dogs: []} where "Dogs" is passed in as a variable [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
I am trying to do this:
var KEYS = {} ;
KEYS.PHONE_TYPE = 'phone-type';
KEYS.AGENT_TYPE = 'agent-type';
var myAppConfig = {
...
iconMap : {
KEYS.PHONE_TYPE : 'icon-phone',
KEYS.AGENT_TYPE : 'icon-headphones'
};
...
};
But it is failing, with a message: Expected ':' and instead saw '.'.
How can I initialize an object using indirect (non-literal) keynames?
To be clear, the result I want is:
{
'phone-type' : 'icon-phone',
'agent-type' : 'icon-headphones'
}
If you're using ES6 (or something like Babel/browserify), you can write it like this:
iconMap : {
[KEYS.PHONE_TYPE] : 'icon-phone',
[KEYS.AGENT_TYPE] : 'icon-headphones'
};
You would have to add those properties separately using bracket notation:
var myAppConfig = {
...
iconMap : { }
...
};
myAppConfig.iconMap[ KEYS.PHONE_TYPE ] = 'icon-phone';
myAppConfig.iconMap[ KEYS.AGENT_TYPE ] = 'icon-headphones';

javascript functions in objects [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I was trying to "put a function into an object" so I wanted to do something like this but I'm getting errors everywhere.
var someobject = {
makename(1) : null,
makename(2) : null,
makename(3) : null,
makename(4) : null
};
function makename(num) {
return (identifier + ' Bot' + num)
}
In modern (ES2015) JavaScript environments, you can do this:
var someobject = {
[makename(1)]: "foo",
[makename(2)]: "bar"
};
The [ ] wrapper around the property name allows it to be an arbitrary expression. The result of evaluating the expression is interpreted as a string and used as the property name.
var someobject = {}
someObject[makename(1)] = null;
someObject[makename(2)] = null;
someObject[makename(3)] = null;
someObject[makename(4)] = null;
This works everywhere. However, #pointy's solution is nicer!

Return part of string with RegEx [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
First of all, Sorry, I'm newbie in Regular Expression so I cannot found the format for my RegEx.
I have a string like this :
{
"xs:schema" : {
"xs:element" : {
"xs:complexType" : {
"xs:choice" : {
"xs:element" : {
"xs:complexType" : {
"xs:sequence" : {
"xs:element":null
}
}
}
}
}
}
},
"diffgr:diffgram" : {
"NewDataSet" : {
"Table" : {
"Item1" : 12345,
"Item2":"Part"
}
}
}
}
I want select just this part of my string:
"NewDataSet" : {
"Table" : {
"Item1" : 12345,
"Item2":"Part"
}
}
how can I do it ?
You're going to solve this easily just using JSON serialization. For example:
var obj = JSON.parse(jsonText);
var partOfJson = JSON.stringify(obj["diffgr:diffgram"]);
OP said somewhere:
Thanks but JSON name is diffgr:diffgram I don't know how can I call
this !
In JavaScript, objects can be used as associative arrays or even dictionaries. That is, their properties can be retrieved using key-value syntax instead of dot syntax.

Categories