I have a json object being returned from a get request in reactjs. I am wanting to get key and value items from this.
{"return":"Success",
"Items": [
{"Name":"centracore", "Type":"rollover" , "Os":"Windows", "Level":"1", "Language_Used":"Assembly", "Size":"4mb"},
{"Name":"centracore", "Type":"Atype" , "Os":"Linux" , "Level":"3", "Language_Used":"C++" , "Size":"4mb"},
{"Name":"centracore", "Type":"random" , "Os":"OSX" , "Level":"2", "Language_Used":"C" , "Size":"4mb"}
]}
I noticed I can access the values manually using the code below.
Keep in mind, that this.state.stuff is holding my json object. Is my format json format bad for what I am trying to do?
if(this.state.authenticated ){
{this.GetPage.bind(this)}
let x = this.state.stuff; Object.entries(this.state.stuff).map((type,item) => {
console.log(type[1][0])
console.log(type[1][1])
console.log(type[1][2])
})
As far as I understand you want to be able to get the key and values within the Object Items arrays, so you need to map over items array and then the key, values from the obtained item with Object.entries()
You can do it like
if(this.state.authenticated ){
{this.GetPage.bind(this)}
this.state.stuff.Items.map((item, index) => {
Object.entries(item).forEach([key, value], () => {
console.log(key, value)
})
})
Working example
var obj = {"return":"Success",
"Items": [
{"Name":"centracore", "Type":"rollover" , "Os":"Windows", "Level":"1", "Language_Used":"Assembly", "Size":"4mb"},
{"Name":"centracore", "Type":"Atype" , "Os":"Linux" , "Level":"3", "Language_Used":"C++" , "Size":"4mb"},
{"Name":"centracore", "Type":"random" , "Os":"OSX" , "Level":"2", "Language_Used":"C" , "Size":"4mb"}
]}
obj.Items.map((item, index) => {
console.log( item);
Object.entries(item).forEach(([key, value]) => {
console.log(key, value)
})
})
JSFIDDLE
Related
I am using embedding signing from salesforce using apex toolkit. I could not merge fields from salesforce. I am trying to prepopulate firstname and lastname. I tried with customtabs and customfields. Custom tab is repeating the same value for all the tabs and custom field is not working. Can someone help pls.
contact c =[select firstName,lastname, Envelope_Id__c from contact where id = :currentUser.contactId];
string EnvelopeId='';
Id mySourceId = currentUser.ContactId; // The ID of the initiating Salesforce object
String conStr = (String) currentUser.ContactId + '~Contact';
ContractType__mdt ContractType= [SELECT label, Envelope_Configuration__c,External_Document_Id__c
FROM ContractType__mdt where Envelope_Configuration__c =:application.Envelope_Configuration__c limit 1];
string templateId= ContractType.External_Document_Id__c;
/*
dfsle.Envelope dsEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
new dfsle.Entity(mySourceId)) // The initiating Salesforce entity--current SF user (salesperson)
.withDocuments(new List<dfsle.Document> {
dfsle.Document.fromTemplate(dfsle.UUID.parse(templateId), description)
})
.withRecipients(new List<dfsle.Recipient> {
dfsle.Recipient.newEmbeddedSigner() // An embedded signer
}
);
if(!Test.isRunningTest()){
// Send the envelope.
dsEnvelope = dfsle.EnvelopeService.sendEnvelope(
dsEnvelope, // The envelope to send
true // Send now?
);
EnvelopeId= String.valueOf(dsEnvelope.docuSignId);
}*/
dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
new dfsle.Entity(mySourceId));
// The initiating Salesforce entity (an opportunity).
dfsle.Tab myTextFNAMETab = new dfsle.TextTab()
.withRequired(true) // Signer must enter value
.withValue(c.firstName)
.withReadOnly(false)
.withName('FName')
// .withAnchor(new dfsle.Tab.Anchor('Legal Name', true, true, null, true, true, 'pixels', 60, -4));
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
149, // X position
240, // Y position
80, // 100 pixels wide
null)); // Default height
dfsle.Tab myTextLNAMETab = new dfsle.TextTab()
.withRequired(true) // Signer must enter value
.withValue(c.lastName)
.withReadOnly(false)
.withName('LName')
// .withAnchor(new dfsle.Tab.Anchor('Legal Name', true, true, null, true, true, 'pixels', 60, -4));
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
230, // X position
240, // Y position
80, // 100 pixels wide
null)); // Default height
dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
myRecipient2.withTabs(new List<dfsle.Tab> {myTextLNAMETab});
//dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
//add Recipient to the Envelope
myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient2});
myEnvelope = myEnvelope.withEmail('Testt Email Subject', 'Test Email Message');
//myTemplateId contains the DocuSign Id of the DocuSign Template
dfsle.UUID myTemplateId = dfsle.UUID.parse(templateId);
//create a new document for the Envelope
dfsle.Document myDocument = dfsle.Document.fromTemplate(
myTemplateId, // templateId in dfsle.UUID format
'Enrollment Agreement'); // name of the template
dfsle.CustomField myField = new dfsle.CustomField ('text', 'DSFSSourceObjectId', conStr, null, false, false);
//add document to the Envelope
myEnvelope = myEnvelope.withDocuments(new List<dfsle.Document> { myDocument })
.withCustomFields(new List<dfsle.CustomField> {myField});
if(!Test.isRunningTest()){
myEnvelope = dfsle.EnvelopeService.sendEnvelope(
myEnvelope, // The envelope to send
true); // Send now?
I'm assuming you're trying to populate existing fields in your template, if that's the case:
// This will populate all the fields with data label name 'FName':
dfsle.Tab prefill_firstname = new dfsle.TextTab()
.withValue(myContact.firstName)
.withDataLabel('FName');
// This will populate all the fields with data label name 'LName':
dfsle.Tab prefill_lastname = new dfsle.TextTab()
.withValue(myContact.lastName)
.withDataLabel('LName');
// Otherwise, if you just want to a create new recipient's first & last name tabs instead of populating field:
dfsle.Tab New_firstname_tab = new dfsle.FirstNameTab()
.withName('AdditionLastName')
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
400, // X position
240, // Y position
80, // 100 pixels wide
null)); // Default height
dfsle.Tab New_lastname_tab = new dfsle.LastNameTab()
.withName('AdditionLastName')
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
500, // X position
240, // Y position
80, // 100 pixels wide
null)); // Default height
dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
myRecipient2.withTabs(new List<dfsle.Tab> {prefill_firstname,prefill_lastname,New_firstname_tab,New_lastname_tab});
myRecipient2.withRole('Manager'); // remember to specify the role that match the Role in your template.
As for the envelope Custom Fields:
dfsle.CustomField myField = new dfsle.CustomField ('text', 'DSFSSourceObjectId', conStr, null, false, false);
Note: Envelope Custom Fields aren't directly visible to a recipient during a signing session, since you have set the show parameter to 'false', it will not be included in the Certificate of Completion. However, it should still be visible through web console reporting and API:
https://www.docusign.com/blog/dsdev-trenches-tabs-and-custom-fields
I have a directory structures as follows. I want to have only the first path, not the paths which are present after the first file in the report.
I also don't want to check further in that report after getting first file, thus want to save time.
structure for directory:
Report 1
A--B--C--D--E--abc.txt--->needed this
A--B--C--D--E--F--abc.txt avoid this
Report 2
A--B--C--D--E--Q--R--abc.txt needed this, as it is single in its report.
Report 3
A--H--I--J--abc.txt --needed this
Report 4
A--B--C--D--M--abc.txt needed this
A--B--C--D--M--N--abc.txt avoid this
.
.
.
.
millions of such reports.
Directory A contains millions of reports. Each report contains multiple files and subdirectories. Each report have abc.txt in one of the path present
and same path post abc.txt level ,may contain other subdirectories inside the path which also have have abc.txt.
Note:
Reports are of varying level of subdirectories
open my $fh, '-|', 'find', $maindirectory, '-type','d' or die "cant open pipes $! \n";
while (<$fh>) {
my $dirpath = $_;
chomp $dirpath;
if(-d $dirpath) {
$filewithpath = File::Spec->catfile( $dirpath, "abc.txt" );
if (-e $filewithpath) {
push #patharray, $filewithpath;
}
}
}
I think you want the abc.txt files
which are nearest to the
main directory for a common initial path.
That is, you want to avoid looking for
A/B/C/../F/abc.txt if A/B/C/abc.txt
has been found.
This criteria will select files A/H/I/J/abc.txt,
A/B/C/D/M/abc.txt, A/B/C/D/E/abc.txt for your
sample directory tree.
And not A/B/C/D/E/Q/R/abc.txt,
which you have marked as needed in your sample,
because the file A/B/C/D/E/abc.txt has already
been found above it in the directory hierarchy.
You can do this in perl using:
use strict;
use warnings;
use File::Find;
my $maindirectory = "A";
#replace with actual main directory name
File::Find::find(
sub {
if ( -d && -f "$_/abc.txt" ) {
$File::Find::prune = 1;
process_path_of_needed_file("$File::Find::name/abc.txt");
}
},
$maindirectory
);
See https://perldoc.perl.org/File/Find.html
I am trying to send an audio file but I think I am not selecting the file correctly.
I am using react-native-audio-toolkit and I was trying to use the rec object where it records, but that does not seem to work, how could I get the file to send it?
code:
let rec = new Recorder("filename.mp4").record();
// Stop recording after approximately 3 seconds
setTimeout(() => {
rec.stop(err => {
// NOTE: In a real situation, handle possible errors here
let data = new FormData();
data.append("recording[path_to_recording]", rec);
data.append("recording[challenge_id]", 1);
data.append("recording[user_id]", 1);
console.log(data);
axios
.post(config.API_URL + "recordings", data, {
headers: {
Authorization: "Bearer " + this.props.auth.token
}
})
.then(res => res.data);
the log of the recording file looks like this:
Recorder {_path: "filename.mp4", _options: {…}, _recorderId: 0, _state: -2, _duration: -1, …}
_duration
:
-1
_fsPath
:
"/data/user/0/com.cobrn/files/filename.mp4"
_lastSync
:
-1
_options
:
autoDestroy
:
(...)
get autoDestroy
:
ƒ ()
set autoDestroy
:
ƒ ()
proto
:
Object
_path
:
"filename.mp4"
_position
:
-1
_recorderId
:
0
_state
:
-2
canPrepare
:
(...)
canRecord
:
(...)
fsPath
:
(...)
isPrepared
:
(...)
isRecording
:
(...)
state
:
(...)
proto
:
EventEmitter
figured it out, you need the specify the type
data.append("recording[path_to_recording]", {
uri: "file://" + rec._fsPath,
name: "filename.mp4",
type: "audio/mp4"
});
I have a questions regarding threaded comments. Is it possible to order comments DESC and child comments ASC from this query (or table level) or should I make a after query modification?
Below you can find my query that orders all to DESC.
```
$comments = $this->Comments
->find('threaded', ['order' => ['Comments.created' => 'DESC']])
->contain(['Users'])
->matching(
'BoardItems',
function ($q) use ($boardItemId) {
return $q->where(
[
'BoardItems.id' => $boardItemId
]
);
}
)
->all();
```
On SQL Level
You should be able to apply the solution suggested in MySql: ORDER BY parent and child, which
uses COALESCE to group/sort the parents first
groups the children by testing for a non-NULL parent ID
sorts the grouped children
In your case you'd sort by created instead of id, ie something like
ORDER BY
COALESCE(Comments.parent_id, Comments.created) DESC,
Comments.parent_id IS NOT NULL,
Comments.created ASC
To build this in a proper query builder-ish fashion, you'd have to use the order() and orderDesc() methods, so that you can use query expressions, something along the lines of
$query = $this->Comments
->find('threaded');
$comments = $query
// ->contain(...)
// ->matching(...)
// COALESCE(Comments.parent_id, Comments.created) DESC
->orderDesc($query->func()->coalesce([
'Comments.parent_id' => 'identifier',
'Comments.created' => 'identifier'
]))
// Comments.parent_id IS NOT NULL
->order($query->newExpr()->isNotNull('Comments.parent_id'))
// Comments.created ASC
->order(['Comments.created' => 'ASC'])
->all();
See also
Cookbook > Database Access & ORM > Query Builder > Selecting Data
Cookbook > Database Access & ORM > Query Builder > Using SQL Functions
Cookbook > Database Access & ORM > Query Builder > Advanced Conditions
On PHP level
Sorting things afterwards would be an option too, for example using a recursive result formatter:
$sortChildren = function($row) use (&$sortChildren) {
if (!empty($row['children'])) {
$row['children'] =
collection($row['children'])
->sortBy('created', SORT_ASC)
->map($sortChildren)
->toArray();
}
return $row;
};
$comments = $this->Comments
->find('threaded')
// ->contain(...)
// ->matching(...)
->order(['Comments.created' => 'DESC'])
->formatResults(function ($results) use ($sortChildren) {
return $results->map($sortChildren);
})
->all();
This would retrieve everything sort descending, and then sort all children arrays ascending by the created field. Similarily you could sort things before you output/use them in your views, depending on what exactly you're planning to do with the results.
If you want to keep things in the table, you could for example wrap this all up in a custom finder and/or retrieve the sort closure via a method on your table class.
See also
Cookbook > Collections > Sorting
Cookbook > Collections > Iterating
when i want to insert a document in my mongodb with monk wich has an array element of subdocuments, the funcion insert it wrong in the db.
I'm calling the insert function this way:
var OrderItem = [];
OrderItem[0] = {
'model': asd1,
'size' : M,
'color' : Black
};
OrderItem[1] = {
'model': bsa2,
'size' : S,
'color' : Black
};
var newOrdenCompra = {
'fechaCompra' : d,
'items' : OrderItem
};
$.ajax({
type: 'POST',
data: newOrdenCompra,
url: '/order/addordercompra',
dataType: 'JSON'
}).done(function( response )
{
if (response.msg !== '') {
alert('Error: ' + response.msg);
}
});
and then:
/*
* POST to add orden de compra.
*/
router.post('/addordercompra', function(req, res) {
var db = req.db;
var collection = db.get('ordercompra');
collection.insert(req.body, function(err, result){
res.send(
(err === null) ? { msg: '' } : { msg: err }
);
});
});
But what i get into the BD is something like this (the example that i wrote above have less atributes in the item subdocuments):
> db.ordercompra.find().pretty()
{
"_id" : ObjectId("5601b2181430470c1266d415"),
"fechaCompra" : "2015-09-22T16:54:59Z",
"items[0][model]" : "CLUB DE LA PELEA",
"items[0][size]" : "XXXS",
"items[0][color]" : "CHOCOLATE",
"items[0][sena]" : "1200",
"items[0][precio]" : "2600",
"items[0][ordOT]" : "2",
"items[0][id]" : "55f9e402ebfcd9b414339f8f",
"items[1][model]" : "302",
"items[1][size]" : "M",
"items[1][color]" : "NEGRO",
"items[1][sena]" : "0",
"items[1][precio]" : "2100",
"items[1][ordOT]" : "",
"items[1][id]" : "55e76c0d497c742019bbb5f3"
}
>
What can i do to get the structure of an element with an array of subdocuments? am i doing it wrong or is the insert what's failing?
Thanks for the help! and sorry for my bad english.
Nicolas.
I've solved the problem sending the data transformed with JSON.stringify and changed the ajax parameters to dataType : 'text' and adding contentType: 'application/json'. Doing that the insert works perfect.
Thanks for the help!
Example Imgs:
And the BD looks like this: