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)
Related
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.
This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 5 years ago.
let fileName = "test.c";
let testCase = "Case1";
let test = {};
test.fileName = testCase;
console.log(test)
I need fileName property to be dynamic
What is need is, like below
{
"test.c":"Case1"
}
Can any one help me
test.fileName = testCase;
Won't work in this case. Should be
test[fileName] = testCase;
You can use the ES6 computed property syntax:
{
[fileName]: "Case1"
}
This will be interpreted dynamically as:
{
"test.c": "Case1"
}
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);
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] = {};
This question already has an answer here:
Trouble referencing variable in Collections.where method within render function
(1 answer)
Closed 9 years ago.
Can someone point me in the right direction? How do you pass a reference variable in to an object literal in JavaScript? I am using Backbone.js and specifically I am using the collections.where method. So I have something as follows:
var temp = customers.where({num: 10});
However, what if someone has a variable like var x (that changes) and they want to say something like the following:
var temp = customers.where({num: x});
JavaScript won't let you do this, I know. But how is it done or how do you get around it?
You create a closure over x like this:
var x = 10;
var filter = function() { return customers.where({num: x}); };
var temp = filter(); // uses x = 10
x = 20;
temp = filter(); // uses x = 20