How to save a 2D array to a Script Property in Properties Service in Google App Script? - arrays

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

Related

NPM libery to transform JS Object to PacalCaser or etc

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.

Best approach in moving data from one array to a new array in angular ts

.subscribe((dataChart) => {
// console.log(dataChart)
var forxaxis = []
var cd = [dataChart]
// console.log(cd)
cd.forEach(element => {
forxaxis.push(element.strRequestDate)
console.log(forxaxis)
});
},
Im trying to move my data in the first array into a new array so that I can use it with chart.js. but it didnt work.
dataChart contain 2 column of data. i insert dataChart into an array called cd. then i tried to push one of the column from dataChart which is called strRequestDate into a new array called forxaxis but it just didnt work as per expected. the result is as shown in the image attached.
this is how the data look like. it was called by using sharepoint API
error and the data
You can use array.map property here, so you don't need to push data manually from one array to another
I have taken sample data in dataChart array for demonstration purpose only.
let dataChart = [{strRequestId: 1, strRequestDate: 'ABC'}, {strRequestId: 1, strRequestDate: 'PQR'}];
let forxaxis = dataChart.map(x => x.strRequestDate);
console.log(forxaxis);
Demo
Output:

Storing an array in a Firebase Database

I am trying to store an array in the Firebase Database, but I'm not sure how to do it properly.
I am wanting to store an array such as:
var exampleArray = ["item1", "item2", "item3"]
The reason why is I populate a UIPicker with values from an array, and rather than having to push an update to the app each time to add a new value, it would be better if I could just update the database and instantly add the new value to each app.
Is there any way that I could store the values in the database and pull the values from the database and store it into an array as shown above?
Thank you.
The Firebase setValue method will accept an array just the same as a String or Integer. So you can read and write the values in the same way -
var ref: DatabaseReference!
ref = Database.database().reference()
var exampleArray = ["item1", "item2", "item3"]
// Write the array
ref.child("myArray").setValue(exampleArray)
// Read the array
ref.child("myArray").observe(.value) { snapshot in
for child in snapshot.children {
// Add the values to your picker array here
}
}
A simple solution would be read JSON from a Firebase Database Reference containing the array, and deserealize it in swift to a native swift array. See point 2 at https://firebase.google.com/docs/database/#implementation_path

How to map new property values to an array of JSON objects?

I'm reading back record sets in an express server using the node mssql package. Reading back the values outputs an array of Json objects as expected.
Now I need to modify the Email propoerty value of each Json object. So I tried looping through the recordset and changing the value at each index:
var request = new sql.Request(sql.globalConnection);
request.input('p_email', sql.VarChar(50), userEmail);
request.execute('GetDDMUserProfile', function(err, recordsets, returnValue) {
for (var i = 0; i < recordsets.length; i++){
recordsets[i].Email = "joe#gmail.com";
}
console.log(recordsets);
});
But instead of modifying the Emailvalue of each Json object, this code just appends a new email property to the last Json object.
How can you map new property values to an array of JSON objects?
Example output:
An example of the output is shown below, where a new Email property has been added to the end of the array instead of changing each existing property value:
[
[
{
ID:[
4
],
UserName:"Brian",
Email:"joe#gmail.com"
},
{
ID:[
5
],
UserName:"Brian",
Email:"joe#gmail.com"
}
Email:'joe#gmail.com' ]
]
The issue here is that your dataset appears to not be an array of JSON objects but, rather, an array of arrays of JSON objects. If you know for certain that you'll always have only one array in the top-most array, then you can solve the problem like this:
recordsets[0][i].Email = "joe#gmail.com";
to always target the first array in the top-most array. However, if the top-most array could potentially have more than one array, that'll be a different kind of issue to solve.

Meteor/Mongo get a specific document in an array

Considering:
{
docs: Array[{field1, field2}]
}
I know how to find the document(s) containing field1 or field2 with $elemMatch, but my question is how can I get the value of field1 knowing field2?
I also know that in MongoDB you would use $elemMatch in the projection parameter. Or that I can do this in JS with something like _.find(), but the goal is to get only one specific document from Mongo.
Let say for example a document:
{
_id: 1
docs: [{a: 42, b:"stringX"}, {a:0, b:"stringY"}]
}
How can I get the value of a (42) knowing b ("stringX")?
Is there something like: MyCollection.findOne({_id: 1}, projection: {docs:{b: "stringX"}}) ?
On the server you can use this projection:
var a = MyCollection.findOne({'docs.b': "stringX"},{fields: {'docs.$': 1}}).a;
However the $ array projection does not work on the client in minimongo (yet).
Hopefully this is sufficient to get you unstuck.
See related question

Resources