{
"name": "Max",
"value": 107,
"children": [
{
"name": "Don",
"value": 60,
"children" [
{"name": "CC", "value": 25},
{"name": "Jim", "value": 35}
]
},
{
"name": "David",
"value": 47,
"children": [
{"name": "Jeff", "value": 32},
{"name": "Buffy", "value": 15}
]
}
]
}
How do you count the children of the group?
data.children.length;
does not seem to work.
(data is set to equal the js)
What you want is the number of leaves in a tree. You can recursively traverse the tree to do it:
getNumberOfLeaves = function (obj) {
if (obj.children) {
var res = 0;
obj.children.forEach(function(d){res+=getNumberOfLeaves(d)})
return res
}else {
return 1
}
}
jsFiddle: http://jsfiddle.net/chrisJamesC/PVhUn/
Related
I want to filter out only entries from a list "NestedRecords" inside each "Record" entry.
Let's say I have these Id's of nested records which should be returned:
nestedRecordsToBeReturned = [1,3]
This is the given table with records and nestedRecords inside each record:
{
"Records": [
{
"Record": "record1",
"NestedRecords": [
{
"id": 1,
"label": "l1"
},
{
"id": 2,
"label": "l2"
},
{
"id": 3,
"label": "l3"
},
{
"id": 4,
"label": "l4"
},
{
"id": 6,
"label": "l6"
}
]
},
{
"Record": "record2",
"Records": [
{
"id": 1,
"label": "l1"
},
{
"id": 2,
"label": "l2"
},
{
"id": 4,
"label": "l4"
},
{
"id": 7,
"label": "l7"
}
]
},
{
"Record": "record3",
"Records": [
{
"id": 2,
"label": "l2"
},
{
"id": 3,
"label": "l3"
},
{
"id": 9,
"label": "l9"
},
{
"id": 10,
"label": "l10"
}
]
}
]
}
This is what I want to be returned:
{
"Records": [
{
"Record": "record1",
"NestedRecords": [
{
"id": 1,
"label": "l1"
},
{
"id": 3,
"label": "l3"
},
]
},
{
"Record": "record2",
"Records": [
{
"id": 1,
"label": "l1"
},
]
},
{
"Record": "record3",
"Records": [
{
"id": 3,
"label": "l3"
},
]
}
]
}
How do I achieve this with an elastic query?
Is it even possible to filter a list which is embedded in a document?
I have this collection, and I want to change this data in order that the key will be the branches id and the value will be the items:
{
"data": [
{
"id": 5020,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
{
"id": 6,
"title": "vegetable",
},
},
{
"id": 5025,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
}
]
}
I want to group by this data by branches -> id , something like this:
{
"data": [
"7" : [
{
"id": 5020,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
{
"id": 6,
"title": "vegetable",
},
},
{
"id": 5025,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
]
}
],
"6" : [
{
"id": 5020,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
{
"id": 6,
"title": "vegetable",
},
},
],
}
How can I use the group By method of collection in Laravel? I want to have data like this and my data is the type of collection
You can use a callback in groupBy method to provide custom logic.
Example:
collect($data)->groupBy(fn($item, $key) => $item['branches'][0]['id']);
But in this example I want to suggest you to use loop:
$myGroups = [];
foreach($data as $item) {
foreach($item['branches'] as $branch) {
$myGroups[$branch['id']][] = $item;
}
}
I am trying to display a force directed tree. There are 3 models
Company / Division / Department
Each Company has many divisions. Each division belongs to one Company.
Each Division has many departments. Each department belongs to one Division.
amcharts expects a syntax like:
[{
"name": "First",
"children": [{
"name": "A1", "value": 100
}, {
"name": "A2", "value": 60
}, {
"name": "A3", "value": 30
}]
}, {
"name": "Second",
"children": [{
"name": "B1", "value": 135
}, {
"name": "B2", "value": 98
}, {
"name": "B3", "value": 56
}]
}, {
"name": "Third",
"children": [{
"name": "C1", "value": 335
}, {
"name": "C2", "value": 148
}, {
"name": "C3", "value": 126
}, {
"name": "C4", "value": 26
}]
}, {
"name": "Fourth",
"children": [{
"name": "D1", "value": 415
}, {
"name": "D2", "value": 148
}, {
"name": "D3", "value": 89
}, {
"name": "D4", "value": 64
}, {
"name": "D5", "value": 16
}]
}, {
"name": "Fifth",
"children": [{
"name": "E1", "value": 687
}, {
"name": "E2", "value": 148
}]
}]
I am only able to get the first level here:
foreach ($companies as $company) {
$data[] =
(object) [
'name' => $company->name,
'value' => $company->value,
];
}
I am sure that I need to further iterate through $company->devisions and so on but not sure how to build the final construct amcharts is expecting. Any hints?
You can try something in the lines of:
$data = [];
foreach ($companies as $company) {
$childrenArray = [
'name' => $company->name,
];
foreach ($company->divisions as $division) {
$childArray['children'][] = [
'name' => $division->name,
'value' => $division->value,
];
}
$data[] = $childrenArray;
}
return json_encode($data);
The tree struct is below:
type TreeData struct {
Name string `json:"name"`
Depth int `json:"depth"`
Children []TreeData `json:"children"`
}
I have two trees, and I want merge them into one tree.
How can I do that?
If someone could show me your code, I would be very appreciate!!
I wonder if I could use the recursion way to finish the merge?
treeone:
{
"name": ".",
"depth": 1,
"children": [
{
"name": "com",
"depth": 2,
"children": [
{
"name": "didi"
"depth": 3,
"children": [
{
"name": "dev",
"depth": 4,
"children": null
}
]
}
]
}
]
}
treetwo:
{
"name": ".",
"depth": 1,
"children": [
{
"name": "com",
"depth": 2,
"children": [
{
"name": "didi"
"depth": 3,
"children": [
{
"name": "influxdb",
"depth": 4,
"children": [
{
"name": "cluster-one"
"depth": 5
"children": null
}
]
}
]
}
]
}
]
}
merge:
{
"name": ".",
"depth": 1,
"children": [
{
"name": "com",
"depth": 2,
"children": [
{
"name": "didi"
"depth": 3,
"children": [
{
"name": "influxdb",
"depth": 4,
"children": [
{
"name": "cluster-one"
"depth": 5
"children": null
}
]
},
{
"name": "dev",
"depth": 4,
"children": null
}
]
}
]
}
]
}
I find a good solution for golang to create a tree!!
http://blog.csdn.net/xtxy/article/details/50812392
I have a JSON data which in the following format:
[{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
The tree for this JSON data is:
I want to loop through all the nodes and extract the "id" of all data maintaining its parent child hierarchy. How can i do this using recursive function.
The idea is to parse the json structure in ruby.
I just coded a node js code, you can still make it work on a browser in js, but you will need to download the underscore library here.
_und = require('underscore');
data = [{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
function parse_tree_2(n) {
return(_und.map(n, parse_tree));
}
function parse_tree(n) {
if (n['children']) {
return({id: n['id'], children: parse_tree_2(n['children'])});
} else {
return({id: n['id']});
}
}
result = _und.map(data, parse_tree);
console.log("Result: %j", result);
You can put that into a file, and execute it with node (by download underscore with nmp install node).
On plain js it would be something like:
<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript">
data = [{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
function parse_tree_2(n) {
return(_.map(n, parse_tree));
}
function parse_tree(n) {
if (n['children']) {
return({id: n['id'], children: parse_tree_2(n['children'])});
} else {
return({id: n['id']});
}
}
result = _.map(data, parse_tree);
console.log("Result: %j", result);
</script>
Ruby code:
require 'json'
data = <<EOF
[{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
EOF
json = JSON.parse(data)
def parse_tree(n)
if n["children"]
{id: n["id"], children: n['children'].map {|c| parse_tree(c)} }
else
{id: n["id"]}
end
end
result = json.map {|n| parse_tree(n) }
puts result