Adding elements within Object TypeScript - reactjs

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

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
}
],

Format an object to split the header of the data

I have a Typescript project where I want to change the format of a JSON object. The object I receive is an array of objects with the keys and the values, what I want is to separate the keys in the first array and then the data in each array.
The object I currently have is the following:
[
{
"id": "1",
"ser": null,
"IP": null,
"host": "",
"type": "Web",
"auth": ""
},
{
"id": "2",
"ser": null,
"IP": "191.174.230.02",
"host": "",
"type": "Proxy",
"auth": ""
}
]
This is what I want to achieve:
[
"id",
"ser",
"IP",
"host",
"type",
"auth"
],
[
"1",
null,
null,
"",
"Web",
""
],
[
"2",
null,
"191.174.230.02",
"",
"Proxy",
""
]
This is the object that I want to get by adding the values property to it:`
{
"values": [
[
"id",
"ser",
"IP",
"host",
"type",
"auth"
],
[
"1",
null,
null,
"",
"Web",
""
],
[
"2",
null,
"191.174.230.02",
"",
"Proxy",
""
]
]
}
use Object.keys(data[0]) to get the keys of the first element
iterate all elements with Object.values(element) to get each element data
push key and element value to array
Here is the code.
var data = [
{
id: "1",
ser: null,
IP: null,
host: "",
type: "Web",
auth: "",
},
{
id: "2",
ser: null,
IP: "191.174.230.02",
host: "",
type: "Proxy",
auth: "",
},
];
var arr = [Object.keys(data[0])];
for (let element of data) {
arr.push(Object.values(element));
}
var obj = { values: arr };

React Hooks update nested JSON array by index

I have this JSON object and I want to change an attribute inside it. In this case I'm trying to change VALUE inside configs[0].configs[0].value
{
"id": "PAY_TOOL_1",
"name": "PAY_TOOL_1",
"description": "PayTool1",
"state": "ENABLED",
"configs": [
{
"isDefaultConfig": null,
"id": "configId-1",
"name": "configId-1",
"defaultConfig": null,
"description": null,
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl",
"value": "http://localhost:7070/pay/payment/confirm/", <-- WANT TO CHANGE THIS
"defaultValue": null,
"description": null,
"editable": true
},
{
"isEditable": true,
"identifier": null,
"name": "cancelUrl",
"value": "http://localhost:7070/pay/payment/cancel/",
"defaultValue": null,
"description": null,
"editable": true
}
]
},
{
"isDefaultConfig": null,
"id": "configId-2",
"name": "configId-2",
"defaultConfig": null,
"description": null,
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl2",
"value": "http://localhost:7070/pay/payment/confirm/22",
"defaultValue": null,
"description": null,
"editable": true
},
{
"isEditable": true,
"identifier": null,
"name": "cancelUrl2",
"value": "http://localhost:7070/pay/payment/cancel/22",
"defaultValue": null,
"description": null,
"editable": true
}
]
}
]
}
This is the code in REACT HOOKS following this solution
SOLUTION
const [editPaymentTool, setEditPaymentTool] = useState(null);
function handleConfigurationInputs( configIndex, propertyIndex, configId, attributeName, attributeValue) {
console.log("CONFIG_INDEX: "+configIndex+" PROPERTY_INDEX: "+propertyIndex+" CONFIG ID: "+configId+ " NAME: "+attributeName+ " VALUE: "+attributeValue);
console.log(editPaymentTool);
setEditPaymentTool(prev => ({
...prev,
configs:[{
...prev.configs,
configs:[{
...prev.configs[configIndex].configs[propertyIndex],
value:attributeValue
}]
}]
}));
}
and the output produced which is pretty different from the expected above
{
"id": "PAY_TOOL_1",
"name": "PAY_TOOL_1",
"description": "PayTool1",
"state": "ENABLED",
"configs": [
{
"0": {
"isDefaultConfig": null,
"id": "configId-1",
"name": "configId-1",
"defaultConfig": null,
"description": null,
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl",
"value": "http://localhost:7070/pay/payment/confirm/", <-- EXPECTED TO BE CHANGED BUT NOT
"defaultValue": null,
"description": null,
"editable": true
},
{
"isEditable": true,
"identifier": null,
"name": "cancelUrl",
"value": "http://localhost:7070/pay/payment/cancel/",
"defaultValue": null,
"description": null,
"editable": true
}
]
},
"1": {
"isDefaultConfig": null,
"id": "configId-2",
"name": "configId-2",
"defaultConfig": null,
"description": null,
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl2",
"value": "http://localhost:7070/pay/payment/confirm/22",
"defaultValue": null,
"description": null,
"editable": true
},
{
"isEditable": true,
"identifier": null,
"name": "cancelUrl2",
"value": "http://localhost:7070/pay/payment/cancel/22",
"defaultValue": null,
"description": null,
"editable": true
}
]
},
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl",
"value": "http://localhost:7070/pay/payment/confirm/l", <-- THE CHANGED VALUE IS PUT HERE (WRONG)
"defaultValue": null,
"description": null,
"editable": true
}
]
}
]
}
Any help is appreciated
Try this:
newState = _.cloneDeep(editPaymentTool);
newState.configs[0].configs[0].value = newValue;
//more complicated nested updates
...
setEditPaymentTool(newState);

How to create a context variable in the entire multiplied-replies node in Watson Assistant?

To add a context variable in a node with a single reply, I just add the var once in that response and that's fair enough.
But in multiple replies node, I must configure each response and add the context variable in each one with either context or JSON editor.
Can I add that only once?
If not what is the idea behind that?
Why does not even a node general variable option exist besides reply-specific ones?
The multiple response node is a way to easily have different responses nested together. Before this feature you had to create a node for every single response you wanted to do.
This appears to be a side-effect of this, but is easy to resolve.
Create your main node and set your context variable. Then create a child node and have the parent "Skip User Input". The child node should have your multiple responses.
Example workspace:
{
"name": "Example single context for multiresponse node",
"intents": [],
"entities": [],
"language": "en",
"metadata": {
"api_version": {
"major_version": "v1",
"minor_version": "2017-05-26"
}
},
"description": "",
"dialog_nodes": [
{
"type": "response_condition",
"title": null,
"output": {
"generic": [
{
"values": [
{
"text": "two $example"
}
],
"response_type": "text"
}
]
},
"parent": "node_1_1532968597524",
"context": null,
"metadata": {},
"next_step": null,
"conditions": "input.text == \"2\"",
"description": null,
"dialog_node": "node_3_1532968635994",
"previous_sibling": "node_2_1532968631610"
},
{
"type": "response_condition",
"title": null,
"output": {
"generic": [
{
"values": [
{
"text": "one $example"
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": "node_1_1532968597524",
"context": null,
"metadata": {},
"next_step": null,
"conditions": "input.text == \"1\"",
"description": null,
"dialog_node": "node_2_1532968631610",
"previous_sibling": null
},
{
"type": "standard",
"title": "Enter 1 or 2 to trigger.",
"output": {},
"parent": "node_4_1532968810899",
"context": null,
"metadata": {
"_customization": {
"mcr": true
}
},
"next_step": null,
"conditions": "anything_else",
"description": null,
"dialog_node": "node_1_1532968597524",
"digress_out": "allow_all",
"previous_sibling": null
},
{
"type": "standard",
"title": "$example set to value of \"example\"",
"output": {
"generic": [
{
"values": [],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": null,
"context": {
"example": "example"
},
"metadata": {},
"next_step": {
"behavior": "skip_user_input"
},
"conditions": "anything_else",
"description": null,
"dialog_node": "node_4_1532968810899",
"previous_sibling": "Welcome"
},
{
"type": "standard",
"title": "Anything else",
"output": {
"generic": [
{
"values": [
{
"text": "I didn't understand. You can try rephrasing."
},
{
"text": "Can you reword your statement? I'm not understanding."
},
{
"text": "I didn't get your meaning."
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": null,
"context": null,
"metadata": {},
"next_step": null,
"conditions": "anything_else",
"description": null,
"dialog_node": "Anything else",
"previous_sibling": "node_4_1532968810899"
},
{
"type": "standard",
"title": "Welcome",
"output": {
"generic": [
{
"values": [
{
"text": "Hello. How can I help you?"
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": null,
"context": null,
"metadata": {},
"next_step": null,
"conditions": "welcome",
"description": null,
"dialog_node": "Welcome",
"previous_sibling": null
}
],
"workspace_id": "9e5b9851-5f0e-4e5f-9523-d2ac7045bf1d",
"counterexamples": [],
"system_settings": {
"tooling": {
"store_generic_responses": true
},
"disambiguation": {
"prompt": "Did you mean:",
"none_of_the_above_prompt": "None of the above"
},
"human_agent_assist": {
"prompt": "Did you mean:"
}
},
"learning_opt_out": false
}

How to Generate a Custom JSON from TreePanel

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

Resources