JavaScript How to filter JavaScript object based on property condition? filter javascript object by value,filter javascript object by key,

I am working on a d3 visualization to create a sunburst. I am also adding a functionality to search for a particular arc so that the sunburst displays arcs with only the searched label. If the initial object is:

{
    "name": "root",
    "children": [
        {
            "name": "A",
            "color": "red",
            "children": [
                {
                    "name": "B",
                    "color": "red"
                }
            ]
        },
        {
            "name": "C",
            "color": "red",
            "children": [
                {
                    "name": "D",
                    "color": "red"
                }
            ]
        },
        {
            "name": "E",
            "color": "red",
            "children": [
                {
                    "name": "B",
                    "color": "red"
                }
            ]
        }
    ]
}

I would like to filter this such that it returns the whole hierarchy which contains the searched name anywhere in the hierarchy. Below is a sample output required when searched for "name":"B"

{
    "name": "root",
    "children": [
        {
            "name": "A",
            "color": "red",
            "children": [
                {
                    "name": "B",
                    "color": "red"
                }
            ]
        },
        {
            "name": "E",
            "color": "red",
            "children": [
                {
                    "name": "B",
                    "color": "red"
                }
            ]
        }
    ]
}

Please tell me if anything else might be needed. Thank you.

Code snippet of how I tried to filter.

var path = svg.selectAll("path")
           .data(partition.nodes(root))
           .enter()
           .append("path")
           .filter(function(d){ 
               return (d.name=="B");
           })
           .attr("d", arc)
           .style("fill", function(d) {
               console.log(d.name);
               return color((d.children ? d : d.parent).name); 
           })

This returns only "name":"B" arc and not the hierarchy.

Answer:1

If you want the .filter() way to work for you, you will have to flatten your JSON data apply a filter and then unflatten it. You can check out flattening and unflattening if you are interested. I personally prefer the depth-first-search (or bfs also works) and pruning where required approach. Lets assume your json data is present in mydata javascript var:

d3.json("jsonData.json",function(error,jsonData){
  if(error)
    return console.warn(error);
  var mydata = jsonData;
  dfs(mydata,'B');
});

Now your dfs() function will look something like this:

function dfs(data,label){
  if(!('children' in data))
    return data.name==label;
  for(var i=0;i<data.children.length;++i)
    if(dfs(data.children[i],label)==flase)
      data.children.splice(i--,1);
  return data.children.length > 0 || data.name==label;
}

What is happening here is that we are doing a depth-first search of the JSON data. The first IF condition checks if we are at the leaf node level and returns false if the 'name' property is anything but 'B'. Next we traverse all children of the current node, call dfs() for each child and prune that child if it returns false. At the end we return false if all children of current node have been pruned and if the 'name' property of current node is not 'B' (the condition that the non-leaf node also has to be pruned from the hierarchy). Hope this helps. Let me know if I missed something.

Answer:2

My hunch is that you want to color the searching node and its parent in sunburst.

To do this i have made a function which recursively colors a node's parent like this:

function colorGraph(node){

    var sel = svg.selectAll("path").filter(function (d) {
        return (node==d);
    });
    sel.style("opacity", 1);
    //if parent present change its opacity
    if(node.parent){
        colorGraph(node.parent)
    }
}

Full working code here

Answer:3

I am currently studying JavaScript and I have the following problem. Is it possible to get only the text from a div which has children inside it? I managed to make it work only for the text which ...

I am currently studying JavaScript and I have the following problem. Is it possible to get only the text from a div which has children inside it? I managed to make it work only for the text which ...

  1. text from which country
  2. text message which
  3. which text from about russell best supports

If the user selects drop down,flight no, only one text field and the corresponding label should come in. If the user selects drop down, all, many text fields should be generated such as for getting ...

If the user selects drop down,flight no, only one text field and the corresponding label should come in. If the user selects drop down, all, many text fields should be generated such as for getting ...

In my html page ther is one load-more class.<p class="load-more">Load more</p>. When user scroll down to this div i need to execute a function . For example i need to alert "Hi". For this ...

In my html page ther is one load-more class.<p class="load-more">Load more</p>. When user scroll down to this div i need to execute a function . For example i need to alert "Hi". For this ...

  1. jquery scroll not working
  2. jquery scroll not working on mobile
  3. jquery scroll not working in chrome
  4. jquery scroll doesn't work
  5. jquery scroll event not working
  6. jquery scroll function not working
  7. jquery scrolltop not working
  8. jquery scroll animate not working
  9. jquery window scroll not working
  10. jquery sortable scroll not working
  11. jquery scroll to element not working
  12. jquery window scroll not working in mobile
  13. jquery smooth scroll not working

I want the input type to be folder and not a single file. How can I select a folder instead of just a single file. Also how can I then access each file in that selected folder. I tried this to select ...

I want the input type to be folder and not a single file. How can I select a folder instead of just a single file. Also how can I then access each file in that selected folder. I tried this to select ...