Struggling with Javascript Object Array adding keys [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 5 years ago.
I have this snippet of code:
var arrLiteData = [];
for(ii=0;ii<10;ii++)
{
arrLiteData.push({ii:{"field1":100,"field2":ii}});
}
...but instead of ii taking the increasing numeric value of ii, the array just holds the actual variable name, like this:
[{"ii":{"field1":100,"field2":0}},{"ii":{"field1":100,"field2":1}}...etc, etc...
What am I doing wrong?
Many thanks.

Quotes are optional for javascript object keys, so
{ii:{"field1":100,"field2":ii}} is the same as
{"ii":{"field1":100,"field2":ii}} or even
{ii:{field1:100,field2:ii}}. They are just need if you have non alphanumeric characters.
To solve this you could either use a computed key if you're transpiling your code or targeting recent navigators:
{[ii]:{"field1":100,"field2":ii}}
Or build the object in two steps:
var arrLiteData = [];
for(ii=0;ii<10;ii++)
{
var obj = {};
obj[ii] = {"field1":100,"field2":ii};
arrLiteData.push(obj);
}

Related

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

Assigning multiple indices at once from one list [duplicate]

This question already has answers here:
JavaScript Object Literals & Array Literals
(4 answers)
Closed 5 years ago.
I have five user variables with different names. I'd like them to end up in an array, and am wondering if I can do the assignment to multiple array indices in a single line. Currently, if I do this
var users = {user5k, user10k, user15k, user20k, user25k};
I can't index it later (says users[0] is undefined). So I want the indices built in to the assignment. Something along these lines:
var users = {};
users[0:4] = {user5k, user10k, user15k, user20k, user25k};
This doesn't seem possible in JavaScript, meaning I have to do this:
var users = {};
users[0] = user5k;
users[1] = user10;
// ... et cetera
Is there a way to accomplish the first solution?
When you use {} you're creating an object, not an array. Use [] for arrays.
var users = [user5k, user10k, user15k, user20k, user25k];
If you want to replace part of an array that already exists, you can use Array.prototype.splice()
var users = [];
users.splice(0, 4, [user5k, user10k, user15k, user20k, user25k]);

Is there any clean way of initializing an object with a variable key? [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.
I have two variable, attribute (e.g. "type") and value (e.g. "car"). I want to make an object where the key is the attribute (and the value is the value). Like this: {"type": "car"}.
When I do
let obj = { attribute: value }
I get
> {"attribute": "car"}
This is easy with two lines, as I can just
let obj = {};
obj[attribute] = value;
However, I'm wondering if there is a clean way of doing this in one line (since I'm a former Rubyist and I like making things clean and precise)?
Computed property names, starting from ES2015 aka ES6.
let a = "type", b = "car";
console.log({[a]: b});

Javascript associative array not working [duplicate]

This question already has answers here:
How do I create a dynamic key to be added to a JavaScript object variable [duplicate]
(2 answers)
Closed 8 years ago.
I have this code, but I want the belongsto var to be retrieved as the key using the var value and not the var name:
var belongsto = panel.attr('data-belongsto');
var panelid = panel.attr('id');
tabValue.push({belongsto:panelid}); console.log(tabValue);
this returns [{'belongsto':'12345'}]
As you can see the the script takes belongsto as the key name but I need it to take the content of the variable.
Any help is appreciated, thanks-
Create the object first
var obj = {};
obj[belongsto] = panelid;
tabValue.push(obj);

Updating a result in JSON with a dynamic variable name [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to update a value in my JSON.
My JSON result looks like this:
results = {"ROWCOUNT":50,"COLUMNS":["PERSONID","NAME"],"DATA":{"PERSONID":["42","43","44"], "NAME":["JOE","TOM","JANE"]}
resultData = results.DATA
In the below code I am looping over the result set and attempting to update a value at a position. I believe it is failing because I am not using dynamic variables correctly.
var columnName = "NAME";
for(i=0; i < results.ROWCOUNT; i++ ){
resultData.columnName[i] = "foo" // failing here due to "columnName" being dynamic.
}
Figured it out.. You have to use array syntax
resultData[columName][i]

Categories