JavaScript converting tree to a nested array - javascript

I have an flowchart input using jquery flowchart library like so:
Flowchart
I get the data representation as this:
{
"operators": {
"0": {
"properties": {
"title": "Start",
"inputs": {},
"outputs": {
"outs": {
"label": "Output (:i)",
"multiple": true
}
},
"class": "start-operator"
},
"top": 0,
"left": 0
},
"1": {
"properties": {
"title": "End",
"inputs": {
"ins": {
"label": "Input (:i)",
"multiple": true
}
},
"outputs": {},
"class": "end-operator"
},
"top": null,
"left": null
},
"37": {
"properties": {
"title": "CHROM",
"inputs": {
"ins": {
"label": "Input (:i)",
"multiple": true
}
},
"outputs": {
"output": {
"label": "Output"
}
}
},
"left": 300,
"top": 0
},
"38": {
"properties": {
"title": "CHROM",
"inputs": {
"ins": {
"label": "Input (:i)",
"multiple": true
}
},
"outputs": {
"output": {
"label": "Output"
}
}
},
"left": 580,
"top": 0
},
"39": {
"properties": {
"title": "REF",
"inputs": {
"ins": {
"label": "Input (:i)",
"multiple": true
}
},
"outputs": {
"output": {
"label": "Output"
}
}
},
"left": 920,
"top": 0
},
"40": {
"properties": {
"title": "REF",
"inputs": {
"ins": {
"label": "Input (:i)",
"multiple": true
}
},
"outputs": {
"output": {
"label": "Output"
}
}
},
"left": 300,
"top": 100
},
"41": {
"properties": {
"title": "REF",
"inputs": {
"ins": {
"label": "Input (:i)",
"multiple": true
}
},
"outputs": {
"output": {
"label": "Output"
}
}
},
"left": 300,
"top": 200
},
"42": {
"properties": {
"title": "REF",
"inputs": {
"ins": {
"label": "Input (:i)",
"multiple": true
}
},
"outputs": {
"output": {
"label": "Output"
}
}
},
"left": 620,
"top": 140
},
"43": {
"properties": {
"title": "POS",
"inputs": {
"ins": {
"label": "Input (:i)",
"multiple": true
}
},
"outputs": {
"output": {
"label": "Output"
}
}
},
"left": 740,
"top": 320
}
},
"links": {
"0": {
"fromOperator": "0",
"fromConnector": "outs",
"fromSubConnector": 0,
"toOperator": 37,
"toConnector": "ins",
"toSubConnector": 0,
"color": " #e53935"
},
"1": {
"fromOperator": "0",
"fromConnector": "outs",
"fromSubConnector": 1,
"toOperator": 40,
"toConnector": "ins",
"toSubConnector": 0,
"color": " #d81b60"
},
"2": {
"fromOperator": "0",
"fromConnector": "outs",
"fromSubConnector": 2,
"toOperator": 41,
"toConnector": "ins",
"toSubConnector": 0,
"color": " #8e24aa"
},
"3": {
"fromOperator": "0",
"fromConnector": "outs",
"fromSubConnector": 3,
"toOperator": 43,
"toConnector": "ins",
"toSubConnector": 0,
"color": " #5e35b1"
},
"4": {
"fromOperator": 37,
"fromConnector": "output",
"fromSubConnector": 0,
"toOperator": 38,
"toConnector": "ins",
"toSubConnector": 0,
"color": " #3949ab"
},
"5": {
"fromOperator": 40,
"fromConnector": "output",
"fromSubConnector": 0,
"toOperator": 42,
"toConnector": "ins",
"toSubConnector": 0,
"color": " #546e7a"
},
"6": {
"fromOperator": 41,
"fromConnector": "output",
"fromSubConnector": 0,
"toOperator": 42,
"toConnector": "ins",
"toSubConnector": 1,
"color": " #039be5"
},
"7": {
"fromOperator": 38,
"fromConnector": "output",
"fromSubConnector": 0,
"toOperator": 39,
"toConnector": "ins",
"toSubConnector": 0,
"color": " #00acc1"
},
"8": {
"fromOperator": 42,
"fromConnector": "output",
"fromSubConnector": 0,
"toOperator": 39,
"toConnector": "ins",
"toSubConnector": 1,
"color": " #00897b"
},
"9": {
"fromOperator": 39,
"fromConnector": "output",
"fromSubConnector": 0,
"toOperator": "1",
"toConnector": "ins",
"toSubConnector": 0,
"color": " #43a047"
},
"10": {
"fromOperator": 43,
"fromConnector": "output",
"fromSubConnector": 0,
"toOperator": "1",
"toConnector": "ins",
"toSubConnector": 1,
"color": " #7cb342"
}
},
"operatorTypes": {}
}
I want to use this for filtering data in tabulator so turn this into this:
[
{
"id": 1,
"pid": null
},
{
"id": 39,
"pid": 1
},
{
"id": 38,
"pid": 39
},
{
"id": 37,
"pid": 38
},
{
"id": 42,
"pid": 39
},
{
"id": 40,
"pid": 42
},
{
"id": 41,
"pid": 42
},
{
"id": 43,
"pid": 1
}
]
Then using this lines I turned into a tree:
const idMapping = data.reduce((acc, el, i) => {
acc[el.id] = i;
return acc;
}, {});
let root;
data.forEach(el => {
// Handle the root element
if (el.pid === null) {
root = el;
return;
}
// Use our mapping to locate the parent element in our data array
const parentEl = data[idMapping[el.pid]];
// Add our current el to its parent's `children` array
parentEl.children = [...(parentEl.children || []), el];
});
Here it is as a tree:
{
"id": 1,
"pid": null,
"children": [
{
"id": 39,
"pid": 1,
"children": [
{
"id": 38,
"pid": 39,
"children": [
{
"id": 37,
"pid": 38
}
]
},
{
"id": 42,
"pid": 39,
"children": [
{
"id": 40,
"pid": 42
},
{
"id": 41,
"pid": 42
}
]
}
]
},
{
"id": 43,
"pid": 1
}
]
}
Hovewer I want to simplify this further to just nested arrays of the id's. Like this:
[
1,
[
[
39,
[
[
38,
[
37
],
],
[
42,
[
40,
41
]
]
]
],
43
]
]
Question is, how to convert from tree to nested array?

You can use recursion to do this; leaf ids are passed upward without an array wrapper while interior node ids are wrapped in an array with a second element for their children.
const tree = { "id": 1, "pid": null, "children": [ { "id": 39, "pid": 1, "children": [ { "id": 38, "pid": 39, "children": [ { "id": 37, "pid": 38 } ] }, { "id": 42, "pid": 39, "children": [ { "id": 40, "pid": 42 }, { "id": 41, "pid": 42 } ] } ] }, { "id": 43, "pid": 1 } ] };
const objTreeToArrTree = node =>
node.children
? [node.id].concat([node.children.map(objTreeToArrTree)])
: node.id
;
console.log(JSON.stringify(objTreeToArrTree(tree), null, 2));

Related

How to get a specific object from whole object?

I am trying to get specific object from a whole object below is the example
by using localStorage.getItem('shop/elasticCache/shirt');
I get below data
{"description":"Tech Shirt","configurable_options":[{"attribute_id":80,"values":[{"value_index":"5176","label":"RUST"}],"label":"Color","attribute_code":"color"},{"attribute_id":125,"values":[{"value_index":"2898","label":"Small"},{"value_index":"2901","label":"Medium"},{"value_index":"2904","label":"Large"}],"label":"Size","attribute_code":"size"}],"tsk":1594790209,"size_options":[2898,2901,2904],"regular_price":28,"final_price":null,"price":28,"color_options":[5176],"special_from_date":null,"id":250659,"category":[{"category_id":2,"name":"Default Category","position":1},{"category_id":3,"name":"Clothing","position":14985},{"category_id":30,"name":"Bottoms","position":798},{"category_id":58,"name":"Leggings","position":1},{"category_id":1130,"name":"Char Test Category","position":30}],"sku":"S155551","product_links":[{"link_type":"related","linked_product_sku":null,"linked_product_type":null,"position":0,"sku":"P100031"},{"link_type":"related","linked_product_sku":null,"linked_product_type":null,"position":0,"sku":"P100031"}
I am trying to get
{"category_id":3,"name":"Clothing","position":14985},{"category_id":30,"name":"Bottoms","position":798},{"category_id":58,"name":"Leggings","position":1},{"category_id":1130,"name":"Char Test Category","position":30}]
Is their any way it can be done ?
const {category} = JSON.parse(localStorage.getItem('shop/elasticCache/shirt');)
or
const {category } = {
"description": "Tech Shirt",
"configurable_options": [{
"attribute_id": 80,
"values": [{
"value_index": "5176",
"label": "RUST"
}],
"label": "Color",
"attribute_code": "color"
}, {
"attribute_id": 125,
"values": [{
"value_index": "2898",
"label": "Small"
}, {
"value_index": "2901",
"label": "Medium"
}, {
"value_index": "2904",
"label": "Large"
}],
"label": "Size",
"attribute_code": "size"
}],
"tsk": 1594790209,
"size_options": [2898, 2901, 2904],
"regular_price": 28,
"final_price": null,
"price": 28,
"color_options": [5176],
"special_from_date": null,
"id": 250659,
"category": [{
"category_id": 2,
"name": "Default Category",
"position": 1
}, {
"category_id": 3,
"name": "Clothing",
"position": 14985
}, {
"category_id": 30,
"name": "Bottoms",
"position": 798
}, {
"category_id": 58,
"name": "Leggings",
"position": 1
}, {
"category_id": 1130,
"name": "Char Test Category",
"position": 30
}],
"sku": "S155551",
"product_links": [{
"link_type": "related",
"linked_product_sku": null,
"linked_product_type": null,
"position": 0,
"sku": "P100031"
}, {
"link_type": "related",
"linked_product_sku": null,
"linked_product_type": null,
"position": 0,
"sku": "P100031"
}]
}
objc={"description":"Tech Shirt","configurable_options":
[{"attribute_id":80,"values":[{"value_index":"5176","label":"RUST"}],
"label":"Color","attribute_code":"color"},
{"attribute_id":125,"values":[{"value_index":"2898","label":"Small"}
,{"value_index":"2901","label":"Medium"},
{"value_index":"2904","label":"Large"}],
"label":"Size","attribute_code":"size"}],
"tsk":1594790209,"size_options":[2898,2901,2904]
,"regular_price":28,"final_price":null,"price":28,
"color_options":[5176],"special_from_date":null,"id":250659,
"category":[{"category_id":2,"name":"Default Category","position":1},
{"category_id":3,"name":"Clothing","position":14985},
{"category_id":30,"name":"Bottoms","position":798},
{"category_id":58,"name":"Leggings","position":1},
{"category_id":1130,"name":"Char Test Category","position":30}],
"sku":"S155551","product_links":
[{"link_type":"related","linked_product_sku":null,"linked_product_type":null,"position":0,"sku":"P100031"},
{"link_type":"related","linked_product_sku":null,"linked_product_type":null,"position":0,"sku":"P100031"}]
}
res = objc["category"]
res.shift()
console.log(res)
$(document).ready(function () {
var jsonObj = {
"description": "Tech Shirt",
"configurable_options": [{
"attribute_id": 80,
"values": [{
"value_index": "5176",
"label": "RUST"
}],
"label": "Color",
"attribute_code": "color"
}, {
"attribute_id": 125,
"values": [{
"value_index": "2898",
"label": "Small"
}, {
"value_index": "2901",
"label": "Medium"
}, {
"value_index": "2904",
"label": "Large"
}],
"label": "Size",
"attribute_code": "size"
}],
"tsk": 1594790209,
"size_options": [2898, 2901, 2904],
"regular_price": 28,
"final_price": null,
"price": 28,
"color_options": [5176],
"special_from_date": null,
"id": 250659,
"category": [{
"category_id": 2,
"name": "Default Category",
"position": 1
}, {
"category_id": 3,
"name": "Clothing",
"position": 14985
}, {
"category_id": 30,
"name": "Bottoms",
"position": 798
}, {
"category_id": 58,
"name": "Leggings",
"position": 1
}, {
"category_id": 1130,
"name": "Char Test Category",
"position": 30
}],
"sku": "S155551",
"product_links": [{
"link_type": "related",
"linked_product_sku": null,
"linked_product_type": null,
"position": 0,
"sku": "P100031"
}, {
"link_type": "related",
"linked_product_sku": null,
"linked_product_type": null,
"position": 0,
"sku": "P100031"
}]
};
for(var i = 1; i < jsonObj.category.length; i++){
console.log(jsonObj.category[i]);
}
});
You can iterate through the entire object and can get the required values. The above code gives the values of category from index 1.

Format data set as expected by endpoint request

I am trying to format data that I send to an endpoint. Currently the endpoint expects a certain format but the data that I am sending does not match that entirely. The data I am sending has extra brackets. Please see my code below versus what it expected.
What I am sending
[
[
{
"corporateId": "97765c76-19c3-48b5-8183-d450e72e8f23",
"selectedMAP": [
{
"mapId": 53,
"mapName": "Discovery",
"active": true,
"options": [
{
"optionId": 81,
"optionName": "Keycare",
"memberAmount": 1000,
"adultDependantAmount": 500,
"childDependantAmount": 500,
"active": true
}
]
},
{
"mapId": 54,
"mapName": "Bestmed",
"active": true,
"options": [
{
"optionId": 83,
"optionName": "Beat 1",
"memberAmount": 1000,
"adultDependantAmount": 500,
"childDependantAmount": 500,
"active": true
},
{
"optionId": 84,
"optionName": "Beat 2",
"memberAmount": 2000,
"adultDependantAmount": 1000,
"childDependantAmount": 1000,
"active": true
}
]
}
]
}
],
{
"gapCoverProviders": [
{
"id": 0,
"name": "a",
"isActive": true,
"gapCoverOptions": [
{
"id": 0,
"name": "b",
"optionPrice": 111,
"isActive": true
}
]
}
]
}
]
What is expected
{
"corporateId": "string",
"active": true,
"selectedMAP": [
{
"mapId": 0,
"mapName": "string",
"active": true,
"options": [
{
"optionId": 0,
"optionName": "string",
"memberAmount": 0,
"adultDependantAmount": 0,
"childDependantAmount": 0,
"active": true
}
]
}
],
"gapCoverProviders": [
{
"id": 0,
"name": "string",
"isActive": true,
"gapCoverOptions": [
{
"id": 0,
"name": "string",
"isActive": true,
"optionPrice": 0
}
]
}
]
}
I build the structure that is being posted as follows. I have 2 models which I then combine into 1 data set.
model 1
export class CompanyMedicalAidProvider {
corporateId: string;
active: boolean = true;
selectedMAP: Array<SelectedMap>;
}
model 2
export class CompanyGapCoverProvider {
gapCoverProviders: Array<GapCoverProviders>;
}
data that gets posted
data = [this.companyMedicalAidProvider, this.companyGapCoverProvider];
Any ideas how i can go about changing the structure? I am stuck on this part.
You can create new Data in required format like this
var data = [
[
{
"corporateId": "97765c76-19c3-48b5-8183-d450e72e8f23",
"selectedMAP": [
{
"mapId": 53,
"mapName": "Discovery",
"active": true,
"options": [
{
"optionId": 81,
"optionName": "Keycare",
"memberAmount": 1000,
"adultDependantAmount": 500,
"childDependantAmount": 500,
"active": true
}
]
},
{
"mapId": 54,
"mapName": "Bestmed",
"active": true,
"options": [
{
"optionId": 83,
"optionName": "Beat 1",
"memberAmount": 1000,
"adultDependantAmount": 500,
"childDependantAmount": 500,
"active": true
},
{
"optionId": 84,
"optionName": "Beat 2",
"memberAmount": 2000,
"adultDependantAmount": 1000,
"childDependantAmount": 1000,
"active": true
}
]
}
]
}
],
{
"gapCoverProviders": [
{
"id": 0,
"name": "a",
"isActive": true,
"gapCoverOptions": [
{
"id": 0,
"name": "b",
"optionPrice": 111,
"isActive": true
}
]
}
]
}
]
var newData = data[0][0]
newData['gapCoverProviders'] = data[1]['gapCoverProviders'];
console.log(newData)

how to calculate x,y and z values from JSON

I have json data format given below. From this data I need to display group11 emotions only in 3D charts.So i need x ,y and z value.how to get these values please give me some logic.we can use anything from this given json.
JSON
{
"status": "success",
"result": {
"duration": "15034.88",
"sessionStatus": "Done",
"analysisSegments": [
{
"offset": 0,
"duration": 10000,
"end": 10000,
"analysis": {
"Temper": {
"Value": "62.00",
"Group": "medium",
"Score": "59.00"
},
"Valence": {
"Value": "96.00",
"Group": "positive",
"Score": "94.00"
},
"Arousal": {
"Value": "98.00",
"Group": "high",
"Score": "97.00"
},
"Vad": {
"Voiced": "70.00"
},
"Mood": {
"Group7": {
"Primary": {
"Id": 1,
"Phrase": "Angry"
},
"Secondary": {
"Id": 3,
"Phrase": "Enthusiastic"
}
},
"Group11": {
"Primary": {
"Id": 11,
"Phrase": "Supremacy, Arrogance"
},
"Secondary": {
"Id": 1,
"Phrase": "Creative, Passionate"
}
},
"Group21": {
"Primary": {
"Id": 9,
"Phrase": "egoism"
},
"Secondary": {
"Id": 18,
"Phrase": "motivation"
}
},
"Composite": {
"Primary": {
"Id": 143,
"Phrase": "Insistence and stubbornness. Possibly childishness."
},
"Secondary": {
"Id": 5,
"Phrase": "Ambitious. Assertiveness to achieve goals."
}
}
}
}
},
{
"offset": 5000,
"duration": 10000,
"end": 15000,
"analysis": {
"Temper": {
"Value": "63.00",
"Group": "medium",
"Score": "57.00"
},
"Valence": {
"Value": "89.00",
"Group": "positive",
"Score": "84.00"
},
"Arousal": {
"Value": "94.00",
"Group": "high",
"Score": "91.00"
},
"Vad": {
"Voiced": "62.00"
},
"Mood": {
"Group7": {
"Primary": {
"Id": 1,
"Phrase": "Angry"
},
"Secondary": {
"Id": 3,
"Phrase": "Enthusiastic"
}
},
"Group11": {
"Primary": {
"Id": 11,
"Phrase": "Supremacy, Arrogance"
},
"Secondary": {
"Id": 6,
"Phrase": "Leadership, Charisma"
}
},
"Group21": {
"Primary": {
"Id": 8,
"Phrase": "dominance"
},
"Secondary": {
"Id": 18,
"Phrase": "motivation"
}
},
"Composite": {
"Primary": {
"Id": 107,
"Phrase": "Possessiveness. Ownership. Authoritative."
},
"Secondary": {
"Id": 41,
"Phrase": "Strong drive."
}
}
}
}
}
],
"analysisSummary": {
"AnalysisResult": {
"Temper": {
"Mode": "medium",
"ModePct": "100.00"
},
"Valence": {
"Mode": "positive",
"ModePct": "100.00"
},
"Arousal": {
"Mode": "high",
"ModePct": "100.00"
}
}
}
}
}
What I have tried
x-Emotion name(Supremacy, Arrogance)
y-Emotion count(how many times repeated)
z-Emotion duration(in minutes)
json parse
var counter4=0;
var val4 =0;
var val4minute =0;
var phrase = rawdata[i]["analysis"]["Mood"]["Group11"]["Primary"]["Phrase"];
if (phrase == "Supremacy, Arrogance") {
counter4++;
val4 = counter4 * 10000;
val4minute = Math.floor(val4 / 60000);
}
Here x is "Supremacy, Arrogance" counter4 is y axis and val4minute is z axis
but this logic is wrong can any one give me a better logic.

jointjs: i want to get the value of source":{"id": from json text

I want to get the value of text in " source":{"id": with JSON from this JSon text :
{
"cells":
[
{
"type": "devs.Model", "size": { "width": 40, "height": 40 },
"inPorts": [""], "outPorts": [""], "position": { "x": 103, "y": 345 },
"angle": 0, "id": "4a8edbca-dd9d-4164-bf0a-fc4cbffdca86", "z": 1,
"attrs": {
".label": { "text": "aa", "ref-x": 0.4, "ref-y": 0.2 },
"rect": { "fill": "#2ECC71" },
".inPorts circle": { "fill": "#16A085", "magnet": "active", "type": "input" },
".outPorts circle": { "fill": "#E74C3C", "type": "output" },
".inPorts>.port0>.port-label": { "text": "" },
".inPorts>.port0>.port-body": { "port": { "id": "in8", "type": "in" } },
".inPorts>.port0": { "ref": ".body", "ref-y": 0.5 },
".outPorts>.port0>.port-label": { "text": "" },
".outPorts>.port0>.port-body": {
"port": { "id": "out9", "type": "out" }
},
".outPorts>.port0": { "ref": ".body", "ref-y": 0.5, "ref-dx": 0 }
}
},
{
"type": "link", "source": {}, "target": {}, "id": "35173392-8b69-44fc-b6f4-f7c8a62319bb", "z": 2, "attrs": {}
},
{
"type": "devs.Model", "size": { "width": 40, "height": 40 },
"inPorts": [""], "outPorts": [""], "position": { "x": 603, "y": 488 },
"angle": 0, "id": "39e8bc7f-0553-4c5a-b198-b948b0905ae7", "z": 3,
"attrs": {
".label": { "text": "aaa", "ref-x": 0.4, "ref-y": 0.2 },
"rect": { "fill": "#2ECC71" },
".inPorts circle": { "fill": "#16A085", "magnet": "active", "type": "input" },
".outPorts circle": { "fill": "#E74C3C", "type": "output" },
".inPorts>.port0>.port-label": { "text": "" },
".inPorts>.port0>.port-body": { "port": { "id": "in15", "type": "in" } },
".inPorts>.port0": { "ref": ".body", "ref-y": 0.5 },
".outPorts>.port0>.port-label": { "text": "" },
".outPorts>.port0>.port-body": { "port": { "id": "out16", "type": "out" } },
".outPorts>.port0": { "ref": ".body", "ref-y": 0.5, "ref-dx": 0 }
}
},
{
"type": "link", "source": { "id": "4a8edbca-dd9d-4164-bf0a-fc4cbffdca86", "selector": "g:nth-child(1) g:nth-child(4) g:nth-child(1) circle:nth-child(1) ", "port": "out9" },
"target": { "id": "39e8bc7f-0553-4c5a-b198-b948b0905ae7", "selector": "g:nth-child(1) g:nth-child(3) g:nth-child(1) circle:nth-child(1) ", "port": "in15" },
"id": "19bfe3a0-bb48-4665-8f2b-807c3bc33451", "embeds": "", "z": 4,
"attrs": { ".marker-target": { "d": "M 10 0 L 0 5 L 10 10 z" } }
}]
}
i do
var t = JSON.stringify(graph )+"";
var obj = jQuery.parseJSON(t);
alert(obj.cells[3].source['.id']);
but it doesn’t work
You don't need to stringify and then re-parse it. Just access the field:
alert(graph.cells[3].source.id);
Remove the . from .id
var obj={"cells":[{"type":"devs.Model","size":{"width":40,"height":40},"inPorts":[""],"outPorts":[""],"position":{"x":103,"y":345},"angle":0,"id":"4a8edbca-dd9d-4164-bf0a-fc4cbffdca86","z":1,"attrs":{".label":{"text":"aa","ref-x":0.4,"ref-y":0.2},"rect":{"fill":"#2ECC71"},".inPorts circle":{"fill":"#16A085","magnet":"active","type":"input"},".outPorts circle":{"fill":"#E74C3C","type":"output"},".inPorts>.port0>.port-label":{"text":""},".inPorts>.port0>.port-body":{"port":{"id":"in8","type":"in"}},".inPorts>.port0":{"ref":".body","ref-y":0.5},".outPorts>.port0>.port-label":{"text":""},".outPorts>.port0>.port-body":{"port":{"id":"out9","type":"out"}},".outPorts>.port0":{"ref":".body","ref-y":0.5,"ref-dx":0}}},{"type":"link","source":{},"target":{},"id":"35173392-8b69-44fc-b6f4-f7c8a62319bb","z":2,"attrs":{}},{"type":"devs.Model","size":{"width":40,"height":40},"inPorts":[""],"outPorts":[""],"position":{"x":603,"y":488},"angle":0,"id":"39e8bc7f-0553-4c5a-b198-b948b0905ae7","z":3,"attrs":{".label":{"text":"aaa","ref-x":0.4,"ref-y":0.2},"rect":{"fill":"#2ECC71"},".inPorts circle":{"fill":"#16A085","magnet":"active","type":"input"},".outPorts circle":{"fill":"#E74C3C","type":"output"},".inPorts>.port0>.port-label":{"text":""},".inPorts>.port0>.port-body":{"port":{"id":"in15","type":"in"}},".inPorts>.port0":{"ref":".body","ref-y":0.5},".outPorts>.port0>.port-label":{"text":""},".outPorts>.port0>.port-body":{"port":{"id":"out16","type":"out"}},".outPorts>.port0":{"ref":".body","ref-y":0.5,"ref-dx":0}}},{"type":"link","source":{"id":"4a8edbca-dd9d-4164-bf0a-fc4cbffdca86","selector":"g:nth-child(1) g:nth-child(4) g:nth-child(1) circle:nth-child(1) ","port":"out9"},"target":{"id":"39e8bc7f-0553-4c5a-b198-b948b0905ae7","selector":"g:nth-child(1) g:nth-child(3) g:nth-child(1) circle:nth-child(1) ","port":"in15"},"id":"19bfe3a0-bb48-4665-8f2b-807c3bc33451","embeds":"","z":4,"attrs":{".marker-target":{"d":"M 10 0 L 0 5 L 10 10 z"}}}]};
alert(obj.cells[3].source['id']);

jointjs: I want to get the value of label from json text

I want to get the value of text a "Label" and the type with JSON from this JSon text :
{
"cells": [{
"type": "devs.Model",
"size": {
"width": 40,
"height": 40
},
"inPorts": [""],
"outPorts": [""],
"position": {
"x": 168,
"y": 99
},
"angle": 0,
"id": "c7cf7b2d-3b54-4dd1-9cbf-7a37a72559fc",
"z": 1,
"attrs": {
".label": {
"text": "aa",
"ref-x": 0.4,
"ref-y": 0.2
},
"rect": {
"fill": "#2ECC71"
},
".inPorts circle": {
"fill": "#16A085",
"magnet": "active",
"type": "input"
},
".outPorts circle": {
"fill": "#E74C3C",
"type": "output"
},
".inPorts>.port0>.port-label": {
"text": ""
},
".inPorts>.port0>.port-body": {
"port": {
"id": "in8",
"type": "in"
}
},
".inPorts>.port0": {
"ref": ".body",
"ref-y": 0.5
},
".outPorts>.port0>.port-label": {
"text": ""
},
".outPorts>.port0>.port-body": {
"port": {
"id": "out9",
"type": "out"
}
},
".outPorts>.port0": {
"ref": ".body",
"ref-y": 0.5,
"ref-dx": 0
}
}
}, {
"type": "link",
"source": {},
"target": {},
"id": "1e977b11-c003-4c22-ba48-c04994f63c79",
"z": 2,
"attrs": {}
}]
}
For the label , I do : document.write(jsonString.cells[0].attrs.label.text);
and for the type : (jsonString.cells[0].attrs.label.text);
var jsonString = JSON.stringify(graph);
document.write(jsonString.cells[0].type);

Categories