This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
Closed 2 years ago.
I'm trying to destructure this object:
{
name: "Bryan",
last-name: "Enid"
}
and this is not possible:
const {name, last-name} = req.body
Is there a way to destructure this, without changing the initial object key name?
You need to rename the variable, because minus is an operator and not part of a variable name.
BTW, name is a property of Window.name. if this is used you need to rename this value as well.
const { name, 'last-name': lastName } = { name: 'foo', 'last-name': 'bar' };
console.log(lastName);
Related
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
How can I refer to a variable using a string containing its name?
(7 answers)
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
Closed 9 months ago.
I am trying to access a property’s value in an object, I know the name of the properties, but not the name of the object (if that makes sense).
This object name is retrieved from an array. Sorry I’m not very good with explaining things, but hopefully an example will help…
So for example…
const names = ["john", "ben", "doe"]
const john = {
age: 33,
hobby: "coding"
};
const ben = {
age: 25,
hobby: "soccer"
};
const doe = {
age: 20,
hobby: "playing games"
};
names.forEach((name) => {
console.log(name.age)
console.log(name.hobby)
});
So as you can see, I know what properties each object has, the object name I also know, but it is being passed in an array of strings.
When I run this code, it will log undefined, how would I be able to retrieve the age and hobby of each of the values in the array?
This question already has answers here:
Accessing or creating nested JavaScript objects with string key without eval
(2 answers)
How to set object property (of object property of..) given its string name in JavaScript?
(16 answers)
Closed 3 years ago.
Problem
I want to build a function that takes in a path to a nested object value and sets that value to 'new value'
const obj = {
foo: {
bar: 'baz'
}
}
// api should look like
changeObjKey(o => o.foo.bar)
//or
changeObjKey('foo.bar')
/*
* expected:
obj = {
foo: {
bar: 'new value'
}
}
*/
What I've tried:
Something like this (which obviously doesn't work)
function changeObjKey(cb){
cb(obj) = 'new value'
}
I don't want to use lodash or the likes, I'm looking for the most minimal solution to this problem. I also don't want to use eval and the api should support both foo.bar and foo[bar] (if its string based)
This question already has answers here:
How to duplicate object properties in another object? [duplicate]
(16 answers)
Closed 8 years ago.
Given this object:
var myObject = {name: 'David', date: '11/13/2014'};
I want my constructor to set its object properties based on myObject:
function myClass(_object){
// set given object's properties here
}
I want to set the properties through a loop, so I wont have to set every property by hand - because the object will be dynamic.
I'm looking for a cross-browser solution.
Perhaps what you want is this:
function MyClass(_object) {
for (var p in _object) {
if (_object.hasOwnProperty(p)) {
this[p] = _object[p];
}
}
}
Don't forget to use new:
var obj = new MyClass(myObject);
Fiddle
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 8 years ago.
I have an object holding some of my program constants so that I can use it in all of the source code files. The constants object is something like this:
CONSTANTS = {
THING_TYPE: 'type',
THING_INFORMATION: 'information',
THING_DESCRIPTION: 'description',
THING_NAME: 'name',
manyOtherConstants
}
And I want to create objects using a similar notation and using the value of the constants as a property of the object; this is what I'm trying to do:
var myObject = {
CONSTANTS.THING_TYPE: 'whateverType',
CONSTANTS.THING_INFORMATION: {
CONSTANTS.THING_DESCRIPTION: 'whateverDescription',
CONSTANTS.THING_NAME: 'whateverName',
}
}
The problem is that I cannot use the constants in that way. Javascript says:
'SyntaxError: missing : after property id'
Is there any way of doing what I am trying to do using that notation? Or is the only thing that I can do is the following?
var myObject = {}
myObject[CONSTANTS.THING_TYPE] = 'whateverType';
myObject[CONSTANTS.THING_INFORMATION] = {};
myObject[CONSTANTS.THING_INFORMATION][CONSTANTS.THING_DESCRIPTION] = 'whateverDescription';
myObject[CONSTANTS.THING_INFORMATION][CONSTANTS.THING_NAME] = 'whateverName';
No you cannot do that using object literal initialization syntax.
So the only way is to use what you do in the second case - using [...] notatin.
This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
Suppose I have an object obj which has some variables in it. What I want to do is to add a new variable which has a name i gave as a parameter.
For example:
var obj=
{
number:8
}
function AddField(field_name,obj)
{
//some magical code here
}
If I call
AddField('name',obj);
i want to be able to do
obj.name='Apple'
after calling this function.
Any ideas?
Use square brackets notation:
obj[field_name] = value;
REF: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vId
Use the bracket notation:
obj[field_name] = void 0;
This creates a new property with an undefined value.
If obj is already defined, you can just do obj.field_name = null or obj["field_name"] = null.
I wouldn't set any value to undefined even though the language allows for that, simply for a semantic issue: undefined means "this property/variable has not been defined" and null means "this property/variable has no value".