Want to convert the javascript Object property key to another case like PascalCase, camelCase, snake_case, or CONSTANT_CASE?
For example,
const value = {prop1:'value 1', prop_2: 'value 2'}
// Input value
// Convert to
// {PROP_1: 'value 1', PROP_2: 'value 2'}
Is they any library or a simple and consistent way to achieve this result?
Need to convert nested property too.
Related
I'm trying to refactor my React frontend code, basically all the hardcoded strings are to come from CMS and for that I have made a custom hook that fetches the string my components need.
I have a constant file which previously used to refer this string file that contained all the hardcoded strings before, now since I have the hook, how do I use it to replace this string file inside my constants file?
export const SCORES_INFORMATION_BY_SEVERITY_LEVEL: Record<string, IScoreInformation> = {
[DepressionLevels.MinimalDepression]: {
score: '0 - 4',
title: 'No or minimal depression',
description: Strings.minimalDepressionDescription,
},
[DepressionLevels.MildDepression]: {
score: '5 - 9',
title: 'Mild depression',
description: Strings.mildDepressionDescription,
},
[DepressionLevels.ModerateDepression]: {
score: '10 - 14',
title: 'Moderate depression',
description: Strings.moderateDepressionDescription,
}
};
The description key here is what getting referenced from the String file as you can see.
What I thought could be a possible solution:
I decouple this part of the file and put it in the component itself that refers it as obviously I can't use hook in the consts file. The problem with that is that this constant is getting used at multiple places and I will end up repeating my code
Just make few another constants for these strings in the constant file itself and leave it be but I'm not sure if it's the right thing to do.
I have difficulty loading certain data from the table to get a json array and include it into the morris js donut (example: https://codepen.io/ncarlucci/pen/JYxQBK)
I want to load from the table subscriber the different names from the column type and count them to get the following array:
[
{value: 50, label: 'typename1'},
{value: 25, label: 'typename2'},
{value: 25, label: 'typename3'},
],
if I do it like this:
$subscriber = Subscribe::select('type')->get()->groupBy('type')
->map(function($subscribe){
return $subscribe->count();
})->toJson();
I get the follow output, but it is false:
{"company":1,"person":16,"user":6}
There might be a nicer way to handle your case, but since you didn't provide more informations about your model or database structure, this should work:
$subscriber = Subscribe::select('type')->get()
->groupBy('type')
->map(function($subscribe, $label) {
return ['value' => $subscribe->count(), 'label' => $label];
})
->values()
->toJson();
The key is to build the inner array element within the map function, then call values() to get rid of the unneded outer label left by the map function.
If you need further explaination, just ask in the comments below
I have a 2D array that I need to save as a property in Google App Script. How would I do this?
var array = [['value 1', 1.0, 'A'],['value 2', 2.0, 'B'],['value 3', 3.0, 'C']]
PropertiesService.getScriptProperties().setProperty('myArray', array)
When I run the code as listed above I get [Ljava.lang.Object;#40ac055f as the value.
When I us array.toString() the property value negates the square brackets.
Thanks in advance!
Note that Properties Store is meant to save specific properties and key values. It's not meant to be a replacement for the entire spreadsheet.
Current quotas1 limit the property value size per key to 9kB and total storage per store to 500kB.
If your requirement doesn't cross the limit described above, the easy way is to use JSON.stringify2, Which will convert it to a parsable string.
var array = [['value 1', 1.0, 'A'],['value 2', 2.0, 'B'],['value 3', 3.0, 'C']];
var jarray = JSON.stringify(array);
PropertiesService.getScriptProperties().setProperty('myArray', jarray);
You can then use JSON.parse3 to retrieve the array
var array = JSON.parse(jarray);
RethinkDB
I want to fetch all the records which have a particular field/attribute matching one of 3 different possible values.
In the example below, I want to see all the records who have status equal to 'Ready', 'Active', or 'Something else'
I would expect to be able to do something like this :
r.db('db').table('table').filter(
function (rec) {
return [ 'Ready', 'Active', 'Something else' ].includes( rec('status') );
}
);
That doesn't error, nor does it return me all the records that have status equal to 'Ready', 'Active', or 'Something else' (it returns no results).
How do I find all records with a field matching any one of the values in an array?
Another solution to this which is closer to your initial hunch is using .contains(...):
r.db('db').table('table').filter(
function (rec) {
return r.expr(['Ready', 'Active', 'Something else']).contains( rec('status'));
}
);
This is likely more efficient than using regex.
Ok, I figured this out after reading a bit more about the ReQL query language.
We don't have a JS function in there, instead it is ReQL, which basically just means you can use .match() and a regular expression.
The regular expression to match a list is simply to seperate the matching strings with bars "|".
So the solution to the above example is
r.db('db').table('table').filter(
function (rec) {
return rec('status').match( "Ready|Active|Something else" );
}
);
There are other answers that deal with using ng-option with an object formatted like so:
$scope.opts = [
{value: 111, text: '1st' },
{value: 222, text: '2nd' }
];
My question is, can it be done with an object formatted like this?
$scope.opts = [
{'111': '1st'},
{'222': '2nd'}
];
Trying it out doesn't work for me.
Any help?
To clarify:
I'd like the options to be like this <option value="111">1st</option, etc, where the browser shows 1st, and the model returns 111. Can this be done with an array of simplified objects?
No, the answer you point out does not say the same thing as you case. You have an array of objects having a single property with different names; the name of the single property is the key, the value is tha value.
The "object" syntax for the select is about an object, i.e. in your case it should be:
$scope.opts = {
'111': '1st',
'222': '2nd'
};
What you lose with this? No guaranteed iteration order.