Can I use Template String on object key in ES6? [duplicate] - ecmascript-6

This question already has answers here:
Template String As Object Property Name
(3 answers)
Closed 4 years ago.
Can I create an object passing the key value as template string?
const key = 'my-key', value = 'my-value'
const obj = {
`${key}`: value
}
Is there an alternative to do this?

You have to use computed property syntax:
const key = 'my-key', value = 'my-value'
const obj = {
[`${key}`]: value
}
Note that if you just want to use a variable as key, you can write [key]: value.

Related

Cut JSON String just to its values RegEx [duplicate]

This question already has answers here:
how to use a regular expression to extract json fields?
(7 answers)
Closed 2 years ago.
I'm trying to cut a JSON String to the values after an insert key in Flutter/Dart.
For example I have this JSON String:
[{"insert":"Test12\n"},{"insert":"Test1","attributes":{"b":true}},{"insert":"\nTest"},{"insert":"\n","attributes":{"block":"quote"}},{"insert":"Test","attributes":{"s":true}},{"insert":"\nTest"},{"insert":"\n","attributes":{"block":"ol"}},{"insert":"Test"},{"insert":"\n","attributes":{"block":"ol"}},{"insert":"Test"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":{"_type":"hr","_inline":false}},{"insert":"\n"},{"insert":"Test","attributes":{"i":true,"u":true,"s":true,"b":true}},{"insert":"\n\n"}]
I want to convert this String to just its values after an insert. So the result would be:
Test12 Test1 Test Test Test Test Test Test
I don't need for example these characters "" , . {} \n
Test stands for different strings.
I played around with some RegEx but I can not get it working. Am I even on the right path with RegEx?
Thanks in advance.
It's json, so you might as well decode it :
var jsonString = '[{"insert":"Test12\n"},{"insert":"Test1","attributes":{"b":true}},{"insert":"\nTest"},{"insert":"\n","attributes":{"block":"quote"}},{"insert":"Test","attributes":{"s":true}},{"insert":"\nTest"},{"insert":"\n","attributes":{"block":"ol"}},{"insert":"Test"},{"insert":"\n","attributes":{"block":"ol"}},{"insert":"Test"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":{"_type":"hr","_inline":false}},{"insert":"\n"},{"insert":"Test","attributes":{"i":true,"u":true,"s":true,"b":true}},{"insert":"\n\n"}]';
jsonString = jsonString.replaceAll('\n', '\\n');
// jsonDecode() will convert the json to objects of primitive types.
var result = jsonDecode(jsonString) as List<dynamic>;
var inserts = [ for(var map in result) map["insert"]];
for(var insert in inserts)
print(insert);

How can I get the value of a dynamic wildcard child key returned through an .onWrite cloud function event?

I have a Firebase cloud function observer set up on a path with the following JSON nesting structure:
$myUID: {
places : {
$placeID: {
$myUID_checkedIn = "false"; [line4]
$someoneElseUID_checkedIn = "true";
},
}
}
The observer is set up to observe :
exports.observePlaces = functions.database.ref('users/{userID}/places/{placeID}').onWrite((change,context)
I'd like to retrieve the value of the dynamic key "$myUID_checkedIn", from the JSON structure in [line4].
I tried using something like:
const uid = context.params.userID; //VALID
const dataSnapshot = change.after.val(); //VALID
const isCheckedIn = dataSnapshot.(`${uid}_checkedIn`); //INVALID [line11]
However, I'm unable to retrieve the value for the dynamic key in [line11] using this approach.
Is this possible to achieve given the way my data is structured?
If so, how should I go about retrieving this value?
You need to use JavaScript proper indexing of property names when they are not literal identifiers. Square brackets are used for this:
const isCheckedIn = datasnapShot[`${uid}_checkedIn`]

Is def x = 1 in Scala a variable declaration or a function? [duplicate]

This question already has answers here:
What is the difference between def foo = {} and def foo() = {} in Scala?
(4 answers)
Closed 7 years ago.
Is def x = 1 a function or a variable declaration? And, what is the difference between:
def x = 1 // REPL x: Int
def x() = 1 // REPL x: () Int
Looks like the first one is a variable definition. Please clarify.
No difference at all. Braces are optional for methods with no arguments in Scala. It is a convention to use them, if the method modifies any kind of state, and to leave the away, if it does not (at the call site too).
Both are method definitions. var x = 1 or val x = 1 would be variable definitions.

Setting Ng-options default value [duplicate]

This question already has answers here:
Working with select using AngularJS's ng-options
(7 answers)
Closed 8 years ago.
How do I set the ng-options default value...
here is my ng-option directive:
ng-options="val as val for val in ticket_group.splits"
use ng-init
<select ng-init="myModel = ticket_group.splits[0]" ng-model="myModel" ng-options="val as val for val in ticket_group.splits"></select>
or set the myModel value inside the controller,
$scope.myModel = ticket_group.splits[0]; // ticket_group.splits[1]; ticket_group.splits[2]; .. or what u need
demo

Property changing null whilst updating a value

i am retreiveing the Data object through a class which is partial class in linq to sql.
i am using the same object model to update the changes. but proprertychanging is always null for this object.
An anonoymous type has been converted into strong object type. This strong type is updated again.
Can anyone explain the reason for this?
Sample Code:
var questions = from Question in _db.QuestionDataSource
join AnswerType in _db.AnswerTypeDataSource on Question.IN_AnswerTypeId equals AnswerType.IN_AnswerTypeId
join Section in _db.SectionDataSource on
........
orderby Section.IN_Order,Question.VC_Code
select new
{
Question.UI_QuestionID,
Question.VC_Description,
};
----------
ICollection<ICheckListInstanceQuestion> checkListQuestions = new
List<ICheckListInstanceQuestion>();
foreach (var question in questions)
{
checkListQuestions.Add(new CheckListInstanceQuestion
{
UI_QuestionID = question.UI_QuestionID,
VC_Description = question.VC_Description,
});
The solution is
the object which created is localized one and it needs to be attached into original object.