Object key name with dash [duplicate] - javascript

This question already has answers here:
Which characters are valid/invalid in a JSON key name?
(4 answers)
Closed 5 years ago.
I have to make an object with - like:
let myObject = {
one-property: 'assignee'
}
but JavaScript does not allow this. So any trick to make this work with -? My whole backend has objects with - on keys.

You have to put the key in quotes
let myObject = {
'one-property': 'assignee'
}

Related

Struggling with Javascript Object Array adding keys [duplicate]

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);
}

Mongoose: How to update value in array at index given from variable? [duplicate]

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.
options.votes is an array.
This code works:
Poll.findOneAndUpdate({title},{$inc:{'options.votes.0':1}}
I need to replace 0 for a variable.
This does not work:
const query = 'options.votes.'+variable
Poll.findOneAndUpdate({title},{$inc:{query:1}}
Thanks for help
It's just an object with a string key
var obj = {};
obj['options.votes.'+variable] = 1;
Poll.findOneAndUpdate({title},{$inc:obj})
or with ES2015 and dynamic keys
const query = 'options.votes.'+variable;
Poll.findOneAndUpdate({title},{$inc:{[query]:1}})

How to use an object's value as the parameter for another object in Javascript? [duplicate]

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.
If I have:
var key = "Section";
course.key = "101";
I get told course.key is unidentified. I'm trying to set course.Section = "101". Is there anyway to pass key's value in the second line?
The property key of course is undefined. Use bracket notation.
course[key] = "101"
is the same as
course["Section"] = "101"

How to convert string to object in Angularjs [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 6 years ago.
I have a string like :
$scope.text = '"{\"firstName\":\"John\",\"age\":454 }"';
and I want to convert to js object:
$scope.tmp = {"firstName":"John","age":454 };
Note: JSON.parse() doesn't work!!
It's my sample in codepen
You can do it with angular.fromJson()
in your sample, it would have been $scope.tmp = angular.fromJson($scope.text);
The difference between JSON.Parse() and angular.fromJson, is that angular will check to make sure a string is provided. If it is already an object, it will return the same object.

How would this be done in JavaScript. Using a variable in a concatenate string [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 6 years ago.
Ok. The code here is in fact Javascript but I can not find a way to fixing the problem in javascript. The code below is javascript.
var =inputdata;
dataout = data.items.inputdata.time
This is how it would sort of look like if it was php
dataout = data.items.$inputdata.time
I would like inputdata to be treated as a variable and not as text.
Sorry for the small amount detail
You can use square bracket notation:
dataout = data.items[inputdata].time
This will allow you to use a string in place of a key for a javascript object.

Categories