Translating literal object into object's properties [duplicate] - javascript

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

Related

change nested object value with a user specified path [duplicate]

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)

How to access Object's property itself while defining it? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 4 years ago.
I am trying to dynamically use the previous property's value for calculation of next property.
I have a function like this in Typescript:
MacroGenerator(calories) {
this.caloriedata['macroarray'] = [
{
name: 'Low Carb, High Fat',
pmacro: (Math.round(calories*220.46226218100)/100),
pcals: (4*this.caloriedata['macroarray'][0].pmacro), // THIS IS HOW I TRIED ACCESSING THE PROPERTY AND GETTING ERROR
fcals: (calories*0.3),
fmacro: (Math.round(this.caloriedata['macroarray'][0].fcals/9)/100),
ccals: (calories-this.caloriedata['macroarray'][0].pcals-this.caloriedata['macroarray'][0].fcals),
cmacro: (Math.round(this.caloriedata['macroarray'][0].ccals/4)/100),
}
]
}
I suppose the object isn't instantiated as of when I am trying to access.
Is there any way to access it?
You can use Javascript Getters
From MDN
Sometimes it is desirable to allow access to a property that returns a
dynamically computed value, or you may want to reflect the status of
an internal variable without requiring the use of explicit method
calls. In JavaScript, this can be accomplished with the use of a
getter
var obj = {
a: 4,
get b() {
return this.a * 2;
}
}
console.log(obj.b)

Understanding Object.keys in javascript [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I have a following object:
var sample =
{
count: {
'20-01-2015/17': {
'a': 553056,
'b': 622123
},
'20-01-2015/18': {
'a': 519008,
'b': 610474
}
}
}
I want to find all the keys for count property. Also, all the keys for 20-01-2015/17 property also.
var times = Object.keys(sample.count);
console.log(times);
var time = '20-01-2015/17'
//This works
var props = Object.keys(sample.count[time]);
console.log(props);
//But this doesn't work. I am not able to understand that.
props = Object.keys(sample.count.time);
console(props)
I didn't understand why the first approach is working, but not the second one.
Object.keys() is used to ENUMERATE all properties that have their Enumerable flag turned on in the object descriptor including functions, for example, if you do this Objects.keys(Array.prototype) it will print all the functions defined on the prototype property of the Array which is shared by all Array objects.

Self assign properties in an object? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Can I reference other properties during object declaration in JavaScript? [duplicate]
(7 answers)
Closed 8 years ago.
I am trying to initialize an object and and assign a property of its own to one of the properties.
But my syntax is incorrect.i am referring to the following line:
PCMACUrl = genericURL + "/test"
i have tried
testList[0] = {
executionTimeSec:60,
genericURL:"www.gmail.com",
comments: "",
PCMACUrl = genericURL + "/test"
};
After re-reading all together I believe this is what you are looking for:
(added after init, yes, I know, but it's clean and simple :)
var data = {
'PropA': 1,
'PropB': 2,
'PropC': 3
};
data.PropD = data.PropC+5;
console.log(data); //Object {PropA: 1, PropB: 2, PropC: 3, PropD: 8}
Or, another way to look at it:
if it is possible use backend to generate the object you would like
probably you could also just get rid of same data in same object and use it differently when you call it (referencing to first object propertie and adding second on-the-go:
.../ = data.PropC+anotherData.PropA /...

Create javascript object using constants with dots as property [duplicate]

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.

Categories