score:2
Try this way. Find the group using a recursive method and collect leaf nodes using another recursive method.
function getLeafNodes(leafNodes, obj){
if(obj.children){
obj.children.forEach(function(child){getLeafNodes(leafNodes,child)});
} else{
leafNodes.push(obj);
}
}
function findIds(json,name){
if(json.children){
if(json.name==name) {
var leafNodes = [];
getLeafNodes(leafNodes,json);
console.log(leafNodes.map(function(leafNode){ return leafNode.id; })); //Logs leaf node ids to the console
} else {
json.children.forEach(function(child){
findIds(child,name);
});
}
}
}
Execution of following code will print ["014", "288", "223", "244"]
findIds(actualJSON,"group110");
score:0
The following code traverses the tree recursively. If param
has children
, then its children
will be traversed. Otherwise, its id
will be appended to the results
array, thus, at the end results
will contain the id
of the leaves. getResults
returns results
to simplify its usage.
var results = [];
function getResults(param) {
if (!!param.children) {
for (var child in param.children) {
getResults(param.children[child]);
}
} else {
results[results.length] = param.id;
}
return results;
}
score:0
Here is a generic terse recursive answer for finding nodes using some jquery ($.map). Watch out for stack overflows if the data is deep though! Also it will not continue searching inside a matching node for more matching sub nodes, so it's only applicable if the search term does not nest logically. This method makes use of the array flattening feature of $.map.
var found = (function walk(obj, searchKey, searchTerm) {
if(!$.isPlainObject(obj)) return null;
return obj[searchKey] === searchTerm ? [obj] : $.map(obj, function (lev) {
return walk(lev, searchKey, searchTerm);
});
})(data, 'name', 'group110');
Expanding on that to solve the specific problem above...
var found = (function walk(obj, searchTerm) {
if(!$.isPlainObject(obj)) return null;
return obj.name == searchTerm
? $.map(obj.children, function(c){
return $.map(c.children, function(f){ return f.id; }); })
: $.map(obj.children, function (lev) {
return walk(lev, searchTerm); });
})(data, 'group110');
Or rather
var found = (function walk(obj, lambda, term) {
if(!($.isPlainObject(obj) || $.isArray(obj))) return null;
return lambda.call(obj, term)
? $.map(obj.children, function(c){
return $.map(c.children, function(f){ return f.id; }); })
: $.map(obj.children, function (lev) {
return walk(lev, searchTerm); });
})(data, function(a){ return this.name == a; }, 'group110');
Source: stackoverflow.com
Related Query
- how to find all leaves of group with javascript and json
- How do I remove all children elements from a node and then apply them again with different color and size?
- How to replace a d3js nest function with group and rollup
- How to count and convert an array with specific condition javascript
- How to summarize an array with group and rollup from d3-array?
- How to add all but a few columns using javascript and d3
- JS- how to remove duplicate JSON nodes and add one link for all nodes that get merged
- How to find top 5 numbers in a data set and data associated with it?
- How to change JSON data to Javascript array of objects with D3
- How can i bind an json data with a key and Name for display
- How to use D3 to load csv, display table and style it with bootstrap in javascript
- How do you get JSON data and create a line chart with d3.js?
- How to manipulate JSON objects into an array with javascript
- how create a circle group a organization circle image and connect with curve line with D3.js?
- How do I configure Ghost in in dev and production to make a json data available via url to load with d3.json?
- Is this JSON I made using jsonify with flask and python 3 formatted correctly for making a D3 graph? And if not, how should I format it?
- How do I use JSON and return data with D3
- how can i group JSON data and generate a graph of each group in D3js
- Appending and Using JSON files with Javascript code on Ruby on Rails Pages
- How do I create and write to a new file (csv) and have the browser download it with javascript or D3.js?
- how use json in javascript with django
- JavaScript d3 line graph with ordinal axis and json data
- Gremlin: Find all the paths between two nodes and transform the query result into JSON format
- Find and replace in JSON javascript
- How to create a bar chart with C3 that doesn't group all columns in the same set
- How to load and print json array/object in javascript
- How can we create multi column table using multi array JSON using d3 and crossfilter javascript frameworks
- How do I save/export an SVG file after creating an SVG with D3.js (IE, safari and chrome)?
- D3 - how to deal with JSON data structures?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
More Query from same tag
- Edit JSON data structure using JS
- D3 add zoom to circle pack
- D3 line chart same tick on hover gives different values
- Rickshaw smooth update transition
- Initial zoom not working in d3 v4
- How to improve chart generated from D3noob's tutorial using ds.js
- Using React hooks to prevent React re-rendering of a D3 chart
- Transition height onload of d3js rect svg
- How to Create/Show Multiple Iterations of Charts with the same data in One Area with Buttons using D3.js
- D3.js - Cannot read property 'axis' of undefined
- C3, D3 Chart with nested JSON Data
- pykcharts scatter example does not work with local file
- D3 table with Objects along x axis, fields along y axis. Cell coloured if field condition is met
- how do i format the value shown in a d3 graph
- How do I make the height of a horizontal bar chart flexible in order to be resized when new data has been added in?
- Dimplejs Bug. Line does not show up when series is not null
- Formatting issue in D3
- Setting background-color for Forio Contour charts
- D3js not rendering chart properly: Everything shows up as a line
- Zoomable Sunburst with Labels issue
- heatmap in d3js has weird x axis
- d3: multiple lines same graph with varying y domains?
- Add background to svg polyline
- D3 Tree Layout Separation Between Nodes using NodeSize
- d3.js - Irregular layout on column chart when columns represent months
- How to place node title to the left or right of the node in d3 Sankey graph?
- Way to extend y scale to required number in d3 bar chart
- D3 spacing between bars
- D3.js - Add elements in HTML - <script></script> location
- DJ3S - Graticule removed when rotation is done in orthographic projection