Creating object with dynamic key [duplicate] - javascript

This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 8 years ago.
I have to create a object key based on some dynamic value .
Here the value of str will from user to user. The user enters the value of str.
I want to use str value entered by user to create Object.
var str = $('#inputValue');
var obj = new Object();
obj.assignment = new Object();
obj.assignment.name= "assignmentName";
obj.assignment.str = new Object();

Use the brackets syntax:
obj.assignment[str] = new Object();
Side note: you can use the shortcut {} to create new objects. For example:
obj.assignment[str] = {};

New Object:
obj.assignment[str] = {};

Related

Unable to bind javascript variable to JSON object key [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 4 months ago.
I am trying to add a value in place of json object key but it always returns variable name.
My Code:
var projectName='';
let tempArray=[];
let output={};
for(i=0;i<myJsonArray.length;i++){
name = myJsonArray[i].Project;
tempArray.push(myJsonArray[i]);
}
output= {projectName :tempArray};
console.log(JSON.stringify(output));
This returns a JSON as
{"projectName":[{"Day":"MON","Project":"ABC","Billing Rate":"xxx"},{"Day":"TUE","Project":"ABC","Billing Rate":"xyx"}]}
But I need something like this:
{"ABC":[{"Day":"MON","Project":"ABC","Billing Rate":"xxx"},{"Day":"TUE","Project":"ABC","Billing Rate":"xyx"}]}
Can someone help on what I am missing here.
Kind Regards.
You should wrap the project name into [] that would help to make a value become a key
var name = '';
let tempArray = [];
let output = {};
for (i = 0; i < myJsonArray.length; i++) {
name = myJsonArray[i].Project;
tempArray.push(myJsonArray[i]);
}
output = {
[name]: tempArray
};
console.log(JSON.stringify(output));
P/s: I don't see any projectName variable there, so I replace it by name instead.

I want to get size of Javascript object [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 2 years ago.
I created an object in JavaScript and stored some information in the object. I searched a lot for solution, but did not find any solution. I want to get the count/length of object.
Javascript Code:
productDetail = new Object();
productDetail["Code"] = "A104";
productDetail["ProductName"] = "Keyboard";
productDetail["UnitPrice"] = 10;
productDetail["Dealer"] = "AMD";
This is what you are looking for:
Object.keys(productDetail).length
You are after the count of the property keys of the object.
const productDetail = new Object();
productDetail["Code"] = "A104";
productDetail["ProductName"] = "Keyboard";
productDetail["UnitPrice"] = 10;
productDetail["Dealer"] = "AMD";
console.log(Object.keys(productDetail).length)

JAVASCRIPT- How to prevent object from reflecting changes to its all references [duplicate]

This question already has answers here:
How do I correctly clone a JavaScript object?
(81 answers)
Closed 5 years ago.
This is the code:
var master = [];
var obj = {};
obj.name = "A";
master.push(obj);
console.log(master);
obj.name = "B";
master.push(obj);
console.log(master);
Following is the output on console:
The name property should be A at one place and B at another place
But both properties are displayed as B only.
How to avoid this overriding?
That's what you wanted to do. First define a type, then instantiate 2 objects of it and at the end put them in an array and show the array with them and their value.
obj = function(name) {
this.name = name;
}
var a = new obj("A");
var b = new obj("B");
var master = [a,b];
console.log(master);

Create a JSON field according to the value of 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.
Supposing we have JSON data who came as :
msg = {
fieldName: fieldA
};
msg = {
fieldName: fieldB
};
I can receive multiple of this message, but everytime i have a different field, i want it to add in the 'data' json object, as i can access by using this :
data.fieldA
data.fieldB
By which way can i perform this ?
To access a property of an object when the name of the property is something your code figures out at runtime:
var propertyName = someCode();
var propertyValue = someObject[propertyName];
To create an object with an object initializer and a computed property name, you can do this in modern (ES2015) JavaScript:
var propertyName = someCode();
var someObject = {
[propertyName]: someValue
};
Prior to ES2015, you would do this:
var propertyName = someCode();
var someObject = {};
someObject[propertyName] = someValue;

Why this code doesn't work? (Variable objects key and value) [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
var elems = [];
var at1 = $(indx).attr('class'); //or any string
var at2 = $(indx).html(); //or any string
elems.push({
at1: at2
});
The output I get is:
this
Why can not I set the key as a string?
The way you create an object, the key is going to be interpreted literally as "at1" string. In your case you need to do it slightly more verbose:
var at1 = $(indx).attr('class');
var at2 = $(indx).html();
var obj = {};
obj[at1] = at2;
elems.push(obj);
... or using ES2015 computed property name:
var at1 = $(indx).attr('class');
var at2 = $(indx).html();
elems.push({
[at1]: at2
});

Categories

Resources