property is not being assign to java-script object? [duplicate] - javascript

This question already has answers here:
Why can't a property be added to a null value?
(2 answers)
Closed 6 years ago.
property myProperty is not assigned to variable foo, foo is an object.
var foo=null;//null is an object
foo.myProperty = "my value";
console.log(typeof foo.myProperty);

A Javascript object should be declared like this
var myObject = {};
Try to modify you code like
var foo = {};
foo.myProperty = "a string";
console.log(typeof foo.myProperty);

Related

Undefined when referencing an object via another object in JavaScript [duplicate]

This question already has answers here:
JavaScript - Why can't I call a variable "name"?
(2 answers)
Closed 5 years ago.
My question is pretty simple. I'm creating two objects. The second object is referencing an object inside the first object.
var me = {
name: {
first: "justin"
}
};
var name = me.name;
console.log(me.name.first); // "justin"
console.log(name.first); // undefined
Why am I getting undefined in my second console log? Shouldn't I get "justin" instead?
You need to use another name. There is a name variable which is global.
var me = {
name: {
first: "justin"
}
};
var anotherName = me.name;
console.log(me.name.first);
console.log(anotherName.first);

Cannot set property in an object in angular js [duplicate]

This question already has answers here:
JavaScript - cannot set property of undefined
(8 answers)
Closed 6 years ago.
$scope.objectData = {};
$scope.objectData[key]["digits"] = set.first+','+set.second+','+set.third+','+set.fourth;
here key is a numerical value.The error is
TypeError: Cannot set property 'digits' of undefined
You need to set the value of $scope.objectData[key] to an object before you can add more keys to it.
$scope.objectData[key] = {};
$scope.objectData[key]['digits'] = 'foo';
You first have to initalize the object $scope.objectData[key] so the right code would be:
$scope.objectData = {};
$scope.objectData[key] = {};
$scope.objectData[key]["digits"] = set.first+','+set.second+','+set.third+','+set.fourth;

dynamic array only show me the last value [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 8 years ago.
Possible Duplicate:
How do I add a property to a Javascript Object using a variable as the name?
Use variable for property name in JavaScript literal?
Is it possible to add a variable as the property name of an object in JavaScript, like this:
var myVar = "name";
var myObject = {
{myVar}: "value"
};
Edit
With ES6, this is now possible using a ComputedPropertyName, which manifests in the form of the following syntax:
var myVar = "name";
var myObject = {
[myVar]: "value"
};
You can use the [] syntax to use an expression as the property name (compared to the .prop and prop: value syntaxes where they are always treated as strings):
var myObject = {};
var myVar = "name";
myObject[myVar] = "value";
There is no way to use that inside an object literal, though. You have to create the object first and then assign each property separately.
Like this?
var myVar = "name";
var myObject = {};
myObject[myVar] = "value";
Yes, but not directly.
var myVar = "name";
var object = {};
object[myVar] = "value";

What does '|| {}' means [duplicate]

This question already has answers here:
What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?
(8 answers)
Closed 8 years ago.
I stumbled with the following JavaScript code:
var Employee = function (entity) {
var employee = this;
entity = entity || {};
employee.employeeId = entity.EmployeeId;
employee.email = entity.Email;
employee.firstName = entity.FirstName;
employee.lastName = entity.LastName; // ....
But I couldn't understand the following sentence:
entity = entity || {};
|| is the OR statement in JavaScript. Your function receives entity as variable. When entity is null or undefined, your function will fill it up with empty object which is the same as {}.

jQuery expression as object key [duplicate]

This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I need to figure out how to create a dynamic key string for an object. This expression makes JavaScript complain.
return {$(this).val(): true}; // returns an object e.g. {2: true}
What am I doing wrong?
You have to create the object, then use bracket notation for the dynamic key
var obj = {};
var val = $(this).val();
obj[val] = true;
return obj;
or a completely unnecessary one-liner
return (function(o,e) {o[e.value]=true; return o;})({}, this);
The JavaScript object literal syntax {x: y} specifies that x will be a (possibly) quoteless string, and y any value. You can't use this syntax for dynamic keys.
Use this instead:
var foo = {};
foo[$(this).val()] = true;
return foo;

Categories