JS array to tree by path - javascript

this is my first post ever here. I'm trying to make tree from array of objects by path. I'm unable to get second file into the same folder, folders can be nested infinitely. I'm a bit desperate, this is bugging me for a while...
The array: https://textuploader.com/149ku
let result = [];
let level = { result };
notes.forEach(note => {
const pathSplit = note.path.split('/');
pathSplit.reduce((r, name, i, a) => {
if (!r[name]) {
if (i === pathSplit.length - 1) {
r[name] = { result: [{name: note.title}] };
r.result.push({ name, children: r[name].result });
}
else {
r[name] = { result: [] };
r.result.push({ name, children: r[name].result });
}
}
else {
// What's here?
}
return r[name];
}, level)
});
This is how react render looks:
const treeItems = nodes => {
if(nodes){
return nodes.map((node, index) =>{
return (
<NoteTreeItem style={{paddingLeft: '8px'}} key={index} nodeId={(Math.random()).toString()} labelText={node.name} labelIcon={FolderIcon}>
{Array.isArray(node.children[0].children) ?
node.children.map(child => treeItems([child])) :
<NoteTreeItem saveEditNoteHandle={saveEditNoteHandle} note={node.children[0]} key={Math.random()} nodeId={Math.random().toString()}
labelText={node.children[0].name} labelIcon={DescriptionIcon} />}
</NoteTreeItem>)
});
}
};
Missing file
EDIT: React code is not relevant

To write straightforward paths like: Idk.neco.neco, it can be for example:
const root = {};
notes.forEach((note, i) => {
const folders = note.path.split('/');
folders.reduce((folder, folderName, i, arr) => {
// use prefix/suffix char that cant be contained in path to prevent overwriting
folder[folderName] = folder[folderName] || {_name: folderName, _children: []}
if (i === (arr.length - 1)) {
folder[folderName]._children.push(note);
}
return folder[folderName]
}, root)
})
console.log(root.Idk.neco.neco)
<script>
var notes =[
{
"check": false,
"createdAt": "2020-03-14T06:30:36.259Z",
"_id": "5e6c7a0c8506272230e0ab64",
"title": "Firewall",
"desc": "1) Update\n2) Allow ports\n3) chmod shortcuts\n4) bash\n5) something\n6) something else",
"tag": "Work",
"path": "sysadmin/linux",
"remindDate": "2020-03-20T00:00:00.000Z"
},
{
"check": true,
"createdAt": "2020-03-14T06:31:56.999Z",
"_id": "5e6c7a5c8506272230e0ab65",
"title": "Garden work",
"desc": "Help her with garden work and with cooking",
"tag": "Life",
"path": "Help/Mom",
"remindDate": "2020-03-17T00:00:00.000Z"
},
{
"check": true,
"createdAt": "2020-03-14T06:32:40.738Z",
"_id": "5e6c7a888506272230e0ab66",
"title": "Learn Stocks",
"desc": "When to buy/sell and how to predict",
"tag": "Business",
"path": "Money/stocks",
"remindDate": "2020-03-19T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-03-14T06:34:44.629Z",
"_id": "5e6c7b048506272230e0ab69",
"title": "Kombinatorika",
"desc": "Permutace",
"tag": "School",
"path": "Math class/something/another subfolder/topic",
"remindDate": "2020-04-21T00:00:00.000Z"
},
{
"check": true,
"createdAt": "2020-03-14T06:34:58.273Z",
"_id": "5e6c7b128506272230e0ab6a",
"title": "Posloupnost",
"desc": "Geometricka a neco",
"tag": "Work",
"path": "Math class/something/something/different subfolder/topic",
"remindDate": "2020-04-16T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-03-14T06:37:34.568Z",
"_id": "5e6c7bae8506272230e0ab6b",
"title": "FTP server",
"desc": "Allow group A and B to access FTP",
"tag": "Work",
"path": "sysadmin/windows",
"remindDate": "2020-04-14T00:00:00.000Z"
},
{
"check": true,
"createdAt": "2020-03-15T05:29:29.345Z",
"_id": "5e6dbd398c496347ac5e1010",
"title": "PC",
"desc": "Run antivirus",
"tag": "Life",
"path": "Help/Dad",
"remindDate": "2020-04-04T00:00:00.000Z"
},
{
"check": true,
"createdAt": "2020-03-17T09:11:20.558Z",
"_id": "5e70943831441216a49faae4",
"title": "2021",
"desc": "2021",
"tag": "No category",
"path": "No category",
"remindDate": "2021-03-17T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-03-17T18:53:46.443Z",
"_id": "5e711cba9f3edb19a41ca77a",
"title": "17",
"desc": "17",
"tag": "No category",
"path": "17",
"remindDate": "2020-03-17T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-03-19T08:20:53.237Z",
"_id": "5e732b65a059104a01e0c510",
"title": "20",
"desc": "20",
"tag": "Life",
"path": "20",
"remindDate": "2020-03-20T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-03-19T19:02:28.373Z",
"_id": "5e73c1c4129dc33e68995634",
"title": "not 20",
"desc": "not 20",
"tag": "School",
"path": "20",
"remindDate": "2020-03-17T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-03-21T07:18:44.770Z",
"_id": "5e75bfd4baa9fc431475ed7b",
"title": "VIM",
"desc": "learn vim/vi :D",
"tag": "School",
"path": "sysadmin/linux",
"remindDate": "2020-03-28T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-03-25T07:31:24.943Z",
"_id": "5e7b08cc3d441b1f263304ee",
"title": "Sometging",
"desc": "Ckntent\n\n\n",
"tag": "Life",
"path": "Idk/neco/neco",
"remindDate": "2020-03-26T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-04-04T15:07:45.435Z",
"_id": "5e88a2c134a35d05568ac15a",
"title": "first file",
"desc": "first file",
"tag": "No category",
"path": "twofiles/folder",
"remindDate": "2020-04-30T00:00:00.000Z"
},
{
"check": false,
"createdAt": "2020-04-04T15:07:58.312Z",
"_id": "5e88a2ce34a35d05568ac15b",
"title": "second file",
"desc": "second file",
"tag": "No category",
"path": "twofiles/folder",
"remindDate": "2020-05-02T00:00:00.000Z"
},
];
</script>

Related

How to add "collection" field to metadata for Solana NFTs with Visual Studio Code

I'm trying to add the collection field (name and family) to my metadata using VSC. Not sure where to begin or how to do it. Any help appreciated. Thanks!
{
"name": "Solflare X NFT",
"symbol": "",
"description": "Celebratory Solflare NFT for the Solflare X launch",
"seller_fee_basis_points": 0,
"image": "https://www.arweave.net/abcd5678?ext=png",
"animation_url": "https://www.arweave.net/efgh1234?ext=mp4",
"external_url": "https://solflare.com",
"attributes": [
{ "trait_type": "web", "value": "yes" },
{ "trait_type": "mobile", "value": "yes" },
{ "trait_type": "extension", "value": "yes" }
],
"collection": { "name": "Solflare X NFT", "family": "Solflare" },
"properties": {
"files": [
{
"uri": "https://www.arweave.net/abcd5678?ext=png",
"type": "image/png"
},
{
"uri": "https://watch.videodelivery.net/9876jkl",
"type": "unknown",
"cdn": true
},
{ "uri": "https://www.arweave.net/efgh1234?ext=mp4", "type": "video/mp4" }
],
"category": "video",
"creators": [
{ "address": "SOLFLR15asd9d21325bsadythp547912501b", "share": 100 }
]
}
}

How can I take the data from choiceSet in adaptive cards

I want the data (i.e value inside choices array) inside the red choiseSet when selected and clicked on the btn, I kept an image and added action submit as action.submit as that's my usecase, I am pretty much confused how I can do that, Here's the card payload
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.2",
"body": [
{
"type": "Input.ChoiceSet",
"id": "myColor2",
"style": "expanded",
"value": "1",
"choices": [
{
"title": "Red",
"value": "1"
}
]
},
{
"type": "Image",
"altText": "it's a image",
"url": "https://media.istockphoto.com/vectors/bhutanese-ngultrum-btn-vector-id1304476894",
"width": "100px",
"height": "100px",
"horizontalAlignment": "Left",
"selectAction": {
"type": "Action.Submit",
"id": "btn",
"title": "btn"
}
}
]
}
Once submitted by clicking on the icon, the bot should receive an activity of type message but with no text. The data you want is in the value property.
{
"activity": {
"channelData": {
"postBack": true,
"clientActivityID": "<REMOVED>",
"clientTimestamp": "2022-04-05T18:43:17.348Z"
},
"type": "message",
"value": {
"myColor2": "1"
},
"channelId": "emulator",
"from": {
"id": "<REMOVED>",
"name": "",
"role": "user"
},
"locale": "en-us",
"localTimestamp": "2022-04-05T11:43:17-07:00",
"localTimezone": "America/Los_Angeles",
"timestamp": "2022-04-05T18:43:17.408Z",
"id": "<REMOVED>",
"recipient": {
"id": "<REMOVED>",
"name": "Bot",
"role": "bot"
},
"conversation": {
"id": "<REMOVED>"
},
"serviceUrl": "http://localhost:3212"
},
"id": "<REMOVED>",
"timestamp": 1649184197408
}

Find specific array, deep in a nested Mongo collection

I have a nested Mongo collection which looks like the following:
{
"_id": "BaiD76JR3gZFPKchn",
"ownerAdmin": "NsgdNHh84XrBJALAK",
"activities": [
{
"id": "a1",
"email": "itsarmin#gmail.com",
"type": "image",
"description": "Another one",
"createdAt": "2017-10-25T21:39:44.769Z",
"pins":
"action": "https://www.dropbox.com/as"
},
{
"id": "a2",
"email": "itsarmin#gmail.com",
"type": "image",
"description": "Comment This image For me",
"createdAt": "2017-10-25T18:42:00.484Z",
"action": "https://www.dropbox.com/s/bty2ds"
},
{
"id": "a3"
"email": "armin#four1five.com",
"type": "progress",
"description": "dasd",
"createdAt": "2017-10-24T17:28:11.748Z",
"action": 50
}
],
"createdAt": "2017-10-24T17:27:54.828Z"
}
I'm trying to query using Collection.findOne("BaiD76JR3gZFPKchn",), to return an object in "activities" using 2 field "id:a2" AND "type:image", so I end up with :
{
"id": "a2",
"email": "itsarmin#gmail.com",
"type": "image",
"description": "Comment This image For me",
"createdAt": "2017-10-25T18:42:00.484Z",
"action": "https://www.dropbox.com/s/bty2ds"
}
Try adding projection into your query.
db.collection.findOne( { _id: "BaiD76JR3gZFPKchn" },
{ activities: { $elemMatch: { id: "a2",type:"image"} }, _id: 0 })

JavaScript - Picking data with XMLHttpRequest

I'm having problems with my code, where I need to pick data with a JSON query from an API.
Here is the JSON body what I receive from the query:
{
"status": "success",
"reservations": [
{
"id": "38199",
"subject": "Koneiden vaihto",
"modifiedDate": "2017-05-16T12:46:17",
"startDate": "2017-06-21T08:00:00",
"endDate": "2017-06-21T22:00:00",
"resources": [
{
"id": "124",
"type": "room",
"code": "FRAMIF407",
"parent": {
"id": "4",
"type": "building",
"code": "FramiF",
"name": "Frami F"
},
"name": "Frami F407 (atk 34)"
}
],
"description": ""
},
{
"id": "30505",
"subject": "Alumnitapahtuman etukäteisjärjestelyt",
"modifiedDate": "2017-04-19T09:36:02",
"startDate": "2017-06-21T08:00:00",
"endDate": "2017-06-21T22:00:00",
"resources": [
{
"id": "104",
"type": "room",
"code": "FRAMIF144",
"parent": {
"id": "4",
"type": "building",
"code": "FramiF",
"name": "Frami F"
},
"name": "Frami F144 (lasipalatsi 120)"
}
],
"description": ""
},
{
"id": "38864",
"subject": "Koneiden vaihto/säilytystila",
"modifiedDate": "2017-06-21T06:03:07",
"startDate": "2017-06-21T08:00:00",
"endDate": "2017-06-21T22:00:00",
"resources": [
{
"id": "107",
"type": "room",
"code": "FRAMIF211",
"parent": {
"id": "4",
"type": "building",
"code": "FramiF",
"name": "Frami F"
},
"name": "Frami F211 (fysioterapia/teoria)"
}
],
"description": ""
},
{
"id": "38335",
"subject": "Koneiden vaihto",
"modifiedDate": "2017-05-16T12:48:32",
"startDate": "2017-06-21T08:00:00",
"endDate": "2017-06-21T22:00:00",
"resources": [
{
"id": "127",
"type": "room",
"code": "FRAMIF410",
"parent": {
"id": "4",
"type": "building",
"code": "FramiF",
"name": "Frami F"
},
"name": "Frami F410 (atk 34)"
}
],
"description": ""
},
{
"id": "38426",
"subject": "Koneiden vaihto",
"modifiedDate": "2017-05-16T12:49:25",
"startDate": "2017-06-21T08:00:00",
"endDate": "2017-06-21T22:00:00",
"resources": [
{
"id": "128",
"type": "room",
"code": "FRAMIF411",
"parent": {
"id": "4",
"type": "building",
"code": "FramiF",
"name": "Frami F"
},
"name": "Frami F411 (atk 34)"
}
],
"description": ""
},
{
"id": "43898",
"subject": "Varattu plinttien varastointiin",
"modifiedDate": "2017-06-12T08:54:31",
"startDate": "2017-06-21T08:00:00",
"endDate": "2017-06-21T22:00:00",
"resources": [
{
"id": "106",
"type": "room",
"code": "FRAMIF210",
"parent": {
"id": "4",
"type": "building",
"code": "FramiF",
"name": "Frami F"
},
"name": "Frami F210 (teoria 36)"
}
],
"description": ""
},
{
"id": "38267",
"subject": "Koneiden vaihto",
"modifiedDate": "2017-06-21T06:03:07",
"startDate": "2017-06-21T08:00:00",
"endDate": "2017-06-21T22:00:00",
"resources": [
{
"id": "126",
"type": "room",
"code": "FRAMIF409",
"parent": {
"id": "4",
"type": "building",
"code": "FramiF",
"name": "Frami F"
},
"name": "Frami F409 (atk 34)"
}
],
"description": ""
}
]
}
I need to pick up the all the names from the body, which are for example :
"Frami F407 (atk 34)", "Frami F144 (lasipalatsi 120)",
"Frami F211 (fysioterapia/teoria)", "Frami F410 (atk 34)
Here's how I process the data in order to get the names;
var jsonQuery = {
"startDate": startDate,
"endDate": endDate,
"building": [building]
};
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var json = JSON.parse(xhr.responseText);
console.log(xhr.responseText);
var rooms = [];
for (var i = 0; i < json.reservations.length; i++) {
if (json.reservations[i].resources != null) {
for (var j = 0; j < json.reservations[i].resources.length; j++) {
var reservation = json.reservations[i];
var resource = json.reservations[i].resources[j];
if (resource.type === "room") {
if (rooms.indexOf("code")) {
rooms.push(resource.name);
}
}
}
}
}
}
};
xhr.open("POST", "URL", true, "API-key", "");
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(jsonQuery));
This is how it should work : https://jsfiddle.net/p474djan/3/
But the problem is, when I run it through JSON.parse() and check in the console log, it keeps the first two names ("Frami F407 (atk 34)" and "Frami F144 (lasipalatsi 120)") but removes all the rest of the names. Any idea why this is happening?
I am not able to find any issue on your code but here it is my suggestion you can achieve the same via underscore.js without any looping and so on
https://jsfiddle.net/p474djan/5/
document.getElementById("pageOne").innerHTML = _.pluck(_.flatten(_.pluck(json["reservations"], 'resources')), 'name').join("<br/>")
Please have a look in to this

How to loop inside a json file with javascript?

I have a json file returned on my javascript code. The file looks like this :
{
"data": [
{
"id": "594984240522886",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Ducks",
"description": "ducks",
"link": "http://www.facebook.com/album.php?fbid=594984240522886&id=593959083958735&aid=1073741834",
"cover_photo": "594984260522884",
"count": 4,
"type": "normal",
"created_time": "2013-06-13T15:12:22+0000",
"updated_time": "2013-06-13T15:12:40+0000",
"can_upload": false
},
{
"id": "593963787291598",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Profile Pictures",
"link": "http://www.facebook.com/album.php?fbid=593963787291598&id=593959083958735&aid=1073741832",
"cover_photo": "593963797291597",
"count": 1,
"type": "profile",
"created_time": "2013-06-11T16:52:29+0000",
"updated_time": "2013-06-11T16:52:31+0000",
"can_upload": false
},
{
"id": "593963467291630",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Goats",
"description": "goats",
"link": "http://www.facebook.com/album.php?fbid=593963467291630&id=593959083958735&aid=1073741831",
"cover_photo": "593963477291629",
"count": 7,
"type": "normal",
"created_time": "2013-06-11T16:51:56+0000",
"updated_time": "2013-06-11T16:52:02+0000",
"can_upload": false
},
{
"id": "593962700625040",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Dogs",
"description": "dogs",
"link": "http://www.facebook.com/album.php?fbid=593962700625040&id=593959083958735&aid=1073741830",
"cover_photo": "593962710625039",
"count": 10,
"type": "normal",
"created_time": "2013-06-11T16:50:27+0000",
"updated_time": "2013-06-11T16:50:37+0000",
"can_upload": false
},
{
"id": "593961937291783",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Cows",
"description": "Cows",
"link": "http://www.facebook.com/album.php?fbid=593961937291783&id=593959083958735&aid=1073741829",
"cover_photo": "593961983958445",
"count": 5,
"type": "normal",
"created_time": "2013-06-11T16:48:26+0000",
"updated_time": "2013-06-11T16:49:32+0000",
"can_upload": false
}
],
"paging": {
"cursors": {
"after": "NTkzOTYxOTM3MjkxNzgz",
"before": "NTk0OTg0MjQwNTIyODg2"
}
}
}
I would like to loop inside the "data" and see how many different data elements exist(as you see each element has an id , from , name , description..) . How can i do that with javascript?
You can try the following code:
for(i=0;json.data.length;i++){
var element = json.data[i];
}
or also in this other way:
for (i in json.data) {
if (json.data.hasOwnProperty(i)) {
var element = json.data[i];
}
}

Categories