How to Generate a Custom JSON from TreePanel - extjs

I am trying to generate the JSON from an editable treepanel. I am able to generate the JSON , but would like the JSON to have only certain fields .
Here's how I generate the JSON by traversal.
function getNodeList(bfsQueue) {
var node = bfsQueue.pop();
var nodeQueue = [];
for (var ii = 0; ii < node.childNodes.length; ii++) {
bfsQueue.push( node.childNodes[ii] );
nodeQueue.push( node.childNodes[ii] );
}
if (bfsQueue.length === 0) {
return nodeQueue;
} else {
return nodeQueue.concat( getNodeList(bfsQueue) );
}
}
And this is where I call the function in my submit handler.
var startQueue = [];
var nodeList = [];
startQueue.push( tree.getRootNode() );
nodeList.push( tree.getRootNode() );
nodeList = nodeList.concat(getNodeList( startQueue ));
console.dir(nodeList);
for ( var nn = nodeList.length-1; nn >= 0; nn-- ) {
var params = [];
for (var pp in nodeList[nn].data) {
if (pp === "children" || pp === "loader") {continue;}
params.push('"' + pp + '":' + JSON.stringify(nodeList[nn].data[pp]) + '');
}
if ( nodeList[nn].childNodes.length > 0) {
var childList = [];
for (var ii = 0; ii < nodeList[nn].childNodes.length; ii++) {
childList.push( nodeList[nn].childNodes[ii].json );
}
params.push('"children": [' + childList.join(',') + ']');
}
nodeList[nn].json = "{" + params.join(",") + "}";
}
alert("My Root :"+nodeList[0].json);
The JSON generated is this.
{
"text": "Src",
"id": "src",
"expandable": true,
"expanded": true,
"allowDrag": false,
"parentId": null,
"root": true,
"leaf": "",
"depth": 0,
"index": 0,
"checked": null,
"cls": null,
"iconCls": null,
"isLast": true,
"isFirst": true,
"allowDrop": true,
"loaded": true,
"loading": false,
"href": null,
"hrefTarget": null,
"qtip": null,
"qtitle": null,
"children": [
{
"text": "United Kingdom",
"id": "United Kingdom",
"parentId": "src",
"root": "",
"leaf": "",
"depth": 1,
"index": 0,
"expanded": false,
"expandable": true,
"checked": null,
"cls": "",
"iconCls": "",
"isLast": true,
"isFirst": true,
"allowDrop": true,
"allowDrag": true,
"loaded": true,
"loading": false,
"href": "",
"hrefTarget": "",
"qtip": "",
"qtitle": "",
"children": [
{
"text": "London",
"id": "London",
"parentId": "United Kingdom",
"root": "",
"leaf": "",
"depth": 2,
"index": 0,
"expanded": false,
"expandable": true,
"checked": null,
"cls": "",
"iconCls": "",
"isLast": true,
"isFirst": true,
"allowDrop": true,
"allowDrag": true,
"loaded": false,
"loading": false,
"href": "",
"hrefTarget": "",
"qtip": "",
"qtitle": ""
}
]
}
]
}
And I need it to be in this format. Just a few fields not all .
{
"text": "Src",
"id": "src",
"parentId": null,
"root": true,
"leaf": "",
"depth": 0,
"children": [
{
"text": "United Kingdom",
"id": "United Kingdom",
"parentId": "src",
"root": "",
"leaf": "",
"depth": 1,
"children": [
{
"text": "London",
"id": "London",
"parentId": "United Kingdom",
"root": "",
"leaf": "",
"depth": 2
}
]
}
]
}
Please help. Thanks in advance.

Simply select the fields you want in the result.
Example:
function getNodeData(node, fields) {
var data = {};
// loop through desired fields
Ext.each(fields, function(fieldName) {
data[fieldName] = node.get(fieldName);
});
if (node.hasChildNodes()) {
var children = data.children = [];
node.eachChild(function(child) {
children.push(getNodeData(child, fields));
});
}
return data;
}
Usage:
var fields = ['text', 'id', 'parentId', 'root', 'leaf', 'depth'],
nodeList = getNodeData(tree.getRootNode(), fields);

Related

How to populate array inside array of object in mongoose

i have a classShcema,inside my marksSchema i have class property i.e populated from classSchema.note that classSchema has subject and it is populate from subjectSchema.
const classSchemaDef = new mongoose.Schema({
classes:{type:Number},
subject:[{
type:mongoose.Types.ObjectId,
ref:'subject',
require:true,
default:null
}],
section:[String],
created_by:commonSchema.created_by,
},commonSchema.Trigger)
below is the Marksschema that has class in it. the class is populated but subject is not being populate.
const MarksSchemaDef = new mongoose.Schema(
{
student: {
type: mongoose.Types.ObjectId,
required: true,
ref: "student",
default: null,
},
marks_obtain: [
{
class: {
type: mongoose.Types.ObjectId,
ref: "class",
required: true,
default: null,
},
subject: {
type: mongoose.Types.ObjectId,
ref: "subject",
},
marks: {
type: Number,
required: true,
},
created_by: commonSchema.created_by,
},
],
},
commonSchema.Trigger
);
below is the code to retrive data from marksModel and populate the data.
getAllMarks = async (skip, limit) => {
return await MarksModel.find()
.skip(skip)
.limit(limit)
.populate("student")
.populate("marks_obtain.class")
.populate("marks_obtain.class.subject")
.populate("marks_obtain.subject");
}
below is the response data i got
{
"_id": "63ce6eb551a5815f8662fd87",
"student": {
"parentname": {
"fullname": "mohan shrestha",
"phone": 868594
},
"_id": "63c80a2912d13e1af652df71",
"studentname": "mogina shrestha",
"class": 8,
"image": "localhost:3000/pic.jpg",
"address": "dhanusmode",
"phone": 2675442,
"section": "a",
"created_by": null,
"__v": 0
},
"marks_obtain": [
{
"class": {
"_id": "63a6a17590f43440d694cedb",
"classes": 8,
"subject": [
"63a42fbd14407d172f07586d",
"63a42fd814407d172f075873"
],
"section": [
"a",
"b",
"c"
],
"created_by": null,
"__v": 0
},
"subject": {
"_id": "63a42fd814407d172f075873",
"subjectname": "grammer",
"__v": 0
},
"marks": 55,
"created_by": null,
"_id": "63ce6eb551a5815f8662fd88"
}
],
"__v": 0
}
],

Adding elements within Object TypeScript

Hello everyone i have dynamically expanding object which consists of a 2 x dimensional array. I
try to add in field label:{ } new key:value {"hg":"Hg"} object.
object JSON:
[
{
"callbackQueryData": "tNLQy3VcX",
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "Cll8xZbVo6",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
},
"label": {
"en": "New button"
}
},
{
"callbackQueryData": "qntufUVhz",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[
{
"callbackQueryData": "cx46ECYG9",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[
{
"callbackQueryData": "uHp5yd3Li",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[]
]
I try cast it to simple array and via forEach() addressing everyone elements button:any and to add object which i need. But Spread syntax(...) can't find argument forEach().Or I'm doing it completely wrong.
let arrayButton:IBotButton[][] = ([] as IBotButton[][]).concat(...page.callbacks)//create simple array
arrayButton.forEach((button:any)=>{
...button,
label: { ...button.label, [languageSelect]: buttonText }
})
Assume your arrayButton looks like this:
const arrayButton: IBotButton[][] = [
[
{
label: { "en": "new Button-1" }
// ...
},
{
label: { "en": "new Button-2" }
// ...
},
//...
],
[
//...
]
];
And have these variables:
const languageSelect: string = "jp";
const buttonText: string = "new Button-3";
Then you could modify the label by following code:
// arrayButton is 2d array
arrayButton.forEach((array) => {
// array is 1d array
array.forEach((button, buttonIdx) => {
array[buttonIdx] = {
...button,
label: {
...button.label,
[languageSelect]: buttonText,
},
};
});
});

While Loop in Node Red

I have one system that is connected to some sensors. The main job is to send error numbers when one or more of the sensors are on.
In order to print the error description I have one csv-File with two columns, the first one with the numbers and the second one with the description.
In order to make this task I have created one flow in Node-Red:
In one line I save the array with the error numbers coming from the system in one flow variable (RandomNum).
In another line I read the csv-file with the error description, I transform it in an array of objects and then in fuction node I make a search in order to find the error number and the description.
The search function has as entance the array of objects. I save the flow variable (RandomNum) in an array (ranNum) and I define a new array (newMsg) where I save the description of the errors.
Then I use a while-loop in order to go through the array of objects and compare the value of the first column of this array with the number in ranNum. Then I save the description of the error in the array newMsg and I do this while-loop for each value of the ranNum array.
And here is my problem. When I execute the flow, it works correctly for the first value of the array ranNum but only for this. I get only the description of the first number in the ranNum array as payload, it looks like as the while-loop is used only once, and then it breaks and gives me the array newMsg.
When I check in the Conetxt, RandomNum is one array,
An when I ask to get the array ranNum, it's also an array,
I have check the while-loop at least 20-times, and I have did not found why it does not work.
Can anyone help me with this topic? Is there a problem with the flow variable or there is big difference between JavaScript and NodeJS that I did not found?
Here is the flow I have created:
[
{
"id": "b1d8b61d0ed5a5da",
"type": "tab",
"label": "Flow Test",
"disabled": false,
"info": ""
},
{
"id": "d677b342ccc51f1b",
"type": "inject",
"z": "b1d8b61d0ed5a5da",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 140,
"y": 280,
"wires": [
[
"e2d5187d2e705892",
"a167b5a81e0b36ba"
]
]
},
{
"id": "7aa6e0bcbdf67c1c",
"type": "file in",
"z": "b1d8b61d0ed5a5da",
"name": "ReadAlarms",
"filename": "/home/DMT/Dokumente/DB_Test/alarms.csv",
"format": "utf8",
"chunk": false,
"sendError": false,
"encoding": "none",
"allProps": false,
"x": 530,
"y": 280,
"wires": [
[
"f2ab599d26fd7ca5",
"94ae4b1ea29d9fe2"
]
]
},
{
"id": "f2ab599d26fd7ca5",
"type": "debug",
"z": "b1d8b61d0ed5a5da",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 750,
"y": 220,
"wires": []
},
{
"id": "94ae4b1ea29d9fe2",
"type": "csv",
"z": "b1d8b61d0ed5a5da",
"name": "ConvertAlarms",
"sep": ",",
"hdrin": false,
"hdrout": "all",
"multi": "mult",
"ret": "\\n",
"temp": "",
"skip": "0",
"strings": true,
"include_empty_strings": "",
"include_null_values": "",
"x": 760,
"y": 280,
"wires": [
[
"9a3547cc81c2d2bb",
"ff9175d01efa5ce4"
]
]
},
{
"id": "9a3547cc81c2d2bb",
"type": "debug",
"z": "b1d8b61d0ed5a5da",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 990,
"y": 220,
"wires": []
},
{
"id": "1a270df6276485c4",
"type": "debug",
"z": "b1d8b61d0ed5a5da",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1230,
"y": 220,
"wires": []
},
{
"id": "ff9175d01efa5ce4",
"type": "function",
"z": "b1d8b61d0ed5a5da",
"name": "SearchFailureS7",
"func": "let i=0;\nlet ranNum = [];\nranNum = flow.get(\"RandomNum\");\nlet newMsg = [];\n\nwhile (i<ranNum.length){\n\n let n = ranNum[i];\n newMsg[newMsg.length] = {payload: msg.payload[n].col2};\n i = i + 1;\n \n}\n\nreturn newMsg;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1010,
"y": 280,
"wires": [
[
"1a270df6276485c4"
]
]
},
{
"id": "a167b5a81e0b36ba",
"type": "function",
"z": "b1d8b61d0ed5a5da",
"name": "RandomArray",
"func": "const num=[];\nvar i;\n\nfor (i=0; i<3; i++){\n \nnum[i] = Math.floor(Math.random() * 9);\n\n}\n\nrandArray = {payload: num}\n\nreturn randArray;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 340,
"y": 400,
"wires": [
[
"c011fa2387db0d18",
"9c400785174973e9"
]
]
},
{
"id": "c011fa2387db0d18",
"type": "debug",
"z": "b1d8b61d0ed5a5da",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 530,
"y": 340,
"wires": []
},
{
"id": "9c400785174973e9",
"type": "function",
"z": "b1d8b61d0ed5a5da",
"name": "",
"func": "flow.set(\"RandomNum\",msg.payload);\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 520,
"y": 400,
"wires": [
[]
]
},
{
"id": "e2d5187d2e705892",
"type": "delay",
"z": "b1d8b61d0ed5a5da",
"name": "Delay 10ms",
"pauseType": "delay",
"timeout": "10",
"timeoutUnits": "milliseconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 330,
"y": 280,
"wires": [
[
"7aa6e0bcbdf67c1c"
]
]
}
]
So, I finally found it. The payload in the while-loop was breaking the loop immediately. So I changed the code in the while-loop using a similar code I found on the net. Now it works.
Old code:
let i=0;
let ranNum = [];
ranNum = flow.get("RandomNum");
let newMsg = [];
while (i<ranNum.length){
let n = ranNum[i];
newMsg[newMsg.length] = {payload: msg.payload[n].col2};
i = i + 1;
}
return newMsg;
New code
let i=0;
let ranNum = [];
ranNum = flow.get("RandomNum");
let newMsg = [];
while (i<ranNum.length){
let n = ranNum[i];
let description = msg.payload[n].col2;
newMsg.push(description);
i = i + 1;
}
msg.payload = newMsg;
return msg;

Angular - get data from Object Array

Good morning, can anyone advise me? I'm pretty clueless. I've already spent a few hours on it and I don't know how to solve this.
The data is fictitious and the original JSON is far more complex.
JSON
{
"main": [
[
{
"type": "dasdasdasd",
"id": 5,
"content": {
"title": "adadadsad",
"items": [
{
"date": "2012-02-02T11:23:00Z",
"id": 12,
"name": "test",
"isEnabled": false,
"isHighlited": false,
"images": {
"lists": {
"small": [
{
"id": 18,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"original": [
{
"id": 19,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"large": [
{
"id": 22,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"medium": [
{
"id": 23,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
]
}
},
"enum": "LINIE",
"url": "https://test.com"
}
]
}
}
]
],
"second": [
]
}
How can I get from main -> content -> items -> images -> url?
More precisely each images and from that url.
Thank you very much in advance for your help.
It looks like your main array has an array as 1st element. If that is correct, you take the first element of main, and then loop over that.
let jsonObj = {
"main": [
[
{
"type": "dasdasdasd",
"id": 5,
"content": {
"title": "adadadsad",
"items": [
{
"date": "2012-02-02T11:23:00Z",
"id": 12,
"name": "test",
"isEnabled": false,
"isHighlited": false,
"images": {
"lists": {
"small": [
{
"id": 18,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"original": [
{
"id": 19,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"large": [
{
"id": 22,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"medium": [
{
"id": 23,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
]
}
},
"enum": "LINIE",
"url": "https://test.com"
}
]
}
}
]
],
"second": [
]
}
let smallImg = "", originalImg = "", largeImg = "", mediumImg = "";
jsonObj.main[0].forEach(mainItem => {
mainItem.content.items.forEach(item => {
smallImg = item.images.lists.small[0].url;
originalImg = item.images.lists.original[0].url;
largeImg = item.images.lists.large[0].url;
mediumImg = item.images.lists.medium[0].url;
});
});
console.log(smallImg);
console.log(originalImg);
console.log(largeImg);
console.log(mediumImg);

Array parsing and converting to new array List

How to convert below array to specific output?
Input:
[
{
"id": "9664581",
"isSelected": true,
"isExpanded": false,
"disabled": false,
"cells": [
{
"id": "9664581:att_name",
"value": "Test1 att_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_name"
}
},
{
"id": "9664581:att_email",
"value": "test1#gmail.com",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_email"
}
},
{
"id": "9664581:comp_name",
"value": "Test1 comp_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "comp_name"
}
},
{
"id": "9664581:attendee_ctry",
"value": "Test cnt",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "attendee_ctry"
}
},
{
"id": "9664581:sources",
"value": "Test DB",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "sources"
}
}
]
},
{
"id": "9528552",
"isSelected": true,
"isExpanded": false,
"disabled": false,
"cells": [
{
"id": "9528552:att_name",
"value": "Test2 att_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_name"
}
},
{
"id": "9528552:att_email",
"value": "Test2#gmail.com",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_email"
}
},
{
"id": "9528552:comp_name",
"value": "Dsd comp_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "comp_name"
}
},
{
"id": "9528552:attendee_ctry",
"value": "Test2 cnt",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "attendee_ctry"
}
},
{
"id": "9528552:sources",
"value": "Test2 DB",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "sources"
}
}
]
}
]
output should be like
output:
[
{
"id": "9664581",
"name": "Test1 att_name",
"email": test1#gmail.com
},
{
"id": "9528552",
"name": "Test2 att_name",
"email": test2#gmail.com
}
]
You need to understand your data.
It appears that the id comes right off of each object. As for the name and email, those are the values of the first and second cell. This turns into a trivial mapping.
You could also try to look-up the cells by their header:
function transformData(data) {
return data.map(item => {
return {
id : item.id,
name : item.cells.find(cell => cell.info.header.endsWith('name')).value,
email : item.cells.find(cell => cell.info.header.endsWith('email')).value
};
});
}
console.log(transformData(getData()));
function transformData(data) {
return data.map(item => {
return {
id : item.id,
name : item.cells[0].value,
email : item.cells[1].value
};
});
}
function getData() {
return [{
"id": "9664581",
"isSelected": true,
"isExpanded": false,
"disabled": false,
"cells": [{
"id": "9664581:att_name",
"value": "Test1 att_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_name"
}
}, {
"id": "9664581:att_email",
"value": "test1#gmail.com",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_email"
}
}, {
"id": "9664581:comp_name",
"value": "Test1 comp_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "comp_name"
}
}, {
"id": "9664581:attendee_ctry",
"value": "Test cnt",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "attendee_ctry"
}
}, {
"id": "9664581:sources",
"value": "Test DB",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "sources"
}
}]
}, {
"id": "9528552",
"isSelected": true,
"isExpanded": false,
"disabled": false,
"cells": [{
"id": "9528552:att_name",
"value": "Test2 att_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_name"
}
}, {
"id": "9528552:att_email",
"value": "Test2#gmail.com",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_email"
}
}, {
"id": "9528552:comp_name",
"value": "Dsd comp_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "comp_name"
}
}, {
"id": "9528552:attendee_ctry",
"value": "Test2 cnt",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "attendee_ctry"
}
}, {
"id": "9528552:sources",
"value": "Test2 DB",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "sources"
}
}]
}];
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
You could do this:
let output = input.map(v => {
let name = '';
let email = '';
v.cells.map(obj => {
if(obj.info.header === 'att_name') {
name = obj.value
}
if(obj.info.header === 'att_email') {
email = obj.value
}
return
})
return {
"id": v.id,
"name": name,
"email": email
}
})

Resources