Assign variable content to object property name [duplicate] - javascript

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
I want to create a json object in node.js that looks like this;
{ WantThisName: { property_length: 8 } }
Here is my code;
var property_name = "WantThisName"
var length = 8;
var Obj_bin;
Obj_bin= {property_name: {property_length:length} };
console.log (Obj_bin);
The console output is;
{ property_name: { property_length: 8 } }
The problem lies with property_name not getting the contents of the variable property_name.

You can use computed (dynamic) property names:
> foo = "foozz"
'foozz'
> {[foo]: 42}
{ foozz: 42 }
They're part of the Enhanced Object Literals of es2015 (https://github.com/lukehoban/es6features#enhanced-object-literals).
For your example:
> var property_name = "WantThisName";
var property_name = "WantThisName";
undefined
> var length = 8;
undefined
> {[property_name]: { property_length: length}}
{ WantThisName: { property_length: 8 } }
>

try
Obj_bin = {};
Obj_bin[property_name] = { property_length: 8 };

Related

Odd syntax for setting an object's property in JavaScript [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 3 months ago.
What is happening in the code on line 10 ({[last]: newObj}) in the following snippet:
How is JS able to use the value of parameter last instead of using last as the property name?
let first = 'first';
let last = 'last';
function foo(first, last) {
let newObj = {
name: 'newObj'
};
let obj = {};
Object.assign(obj, {[last]: newObj});
return obj;
}
console.log(foo('bye', 'hey')); // { hey: { name: 'newObj' } }
Thanks.

declaring object using variable [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I am trying to use a variable when declaring an object:
var name1 = "object1";
var data1 = 3;
create_object(name1, data1);
function create_object(name, data) {
var x = {
name: data
}
return x
}
I want x to be stored as
var x = {
object1: 3
}
But my function will make
var x = {
name: 3
}
Is there a way to pass a variable when declaring the name of a child inside an object?
Thanks a lot
To specify a name of a property from a variable you need to use the square brackets notation like this:
function create_object(name, data) {
var x = {};
x[name] = data;
return x;
}

How to get zipCode from object? [duplicate]

This question already has answers here:
How do I return the response from an Observable/http/async call in angular?
(10 answers)
Closed 5 years ago.
I have this:
[Object]
0:Object
zipCode:"1232"
Now what i want is to get zipCode from object i tried with this:
Object[0].zipCode but i get message that 0 is undefined
Update:
this.restService.getByParam("findAddress", customerId)
.subscribe(results => {
this.address = results.payload;
});
And then i get that object and now i want to do something like this:
console.log(this.address[0].zipCode
var obj = { 0 : {'zipCode':"1232"} }
for(var key in obj) {
console.log(obj[key].zipCode);
}
here u go :
var foo = { 0 : {'zipCode':"1232"} }
for(var key in foo) {
var value = foo[key];
}
console.log(value); // this give you 'zipCode':"1232"
console.log(value['zipCode']); // this give you "1232"
https://jsfiddle.net/emilvr/9d58ebuy/

Creating an object with dynamic keys [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Here is a function building an object dynamically:
function onEntry(key, value) {
console.log(key) // productName
console.log(value) // Budweiser
const obj = { key: value }
console.log(obj) // { key: "Budweiser" }
}
Expected output is
{ productName: "Budweiser" }
But property name is not evaluated
{ key: "Budweiser" }
How to make property name of an object evaluated as an expression?
Create an object, and set its key manually.
var obj = {}
obj[key] = value
Or using ECMAScript 2015 syntax, you can also do it directly in the object declaration:
var obj = {
[key] = value
}

Assigning an object property as the key of a new property in Javascript [duplicate]

This question already has answers here:
Passing in dynamic key:value pairs to an object literal? [duplicate]
(3 answers)
Closed 8 years ago.
What I am trying to do is take two properties of a single Javascript object, and creat a new array with the first property as a key for the second one.
var optionArray = {}
for (var i = 0; i < this.collection.models.length; i++) {
var f = $('.optionChange:eq('+i+')')[0].value;
if (f === "yes") {
this.collection.models[i].set({"optionValue":"yes"});
}
else{
this.collection.models[i].set({"optionValue":"no"});
}
var option1 = this.collection.models[i].get("optionName");
var option2 = this.collection.models[i].get("optionValue");
var result = option1 + ":" + option2;
optionArray[i] = {
option1 : option2
}
};
console.log(optionArray);
This however only outputs to {option1:"option2 property value"}. The key will not change, it only displays as the word option1. Is there any way to accomplish this?
This is wrong, since you can't use a variable as the property name when you use {} notation:
optionArray[i] = {
option1 : option2
}
Try this instead:
optionArray[i] = {} // Make a new empty object
optionArray[i][option1] = option2;
You have to write it like this:
optionArray[i] = {}
optionArray[i][option1] = option2;

Categories

Resources