jquery - Show link only in JSON -


i have following json string -

{      "fruits":[         {            "name":"apples",          "id":"1"       },       {            "name":"bananas",          "id":"2"       },       {            "name":"oranges",          "id":"3"       },       {            "name":"pears",          "id":"4"       },       {            "name":"grapes",          "id":"5"       },       {            "name":"strawberries",          "id":"6"       }    ],    "links":[         {            "source":"1",          "target":"2"       },       {            "source":"1",          "target":"3"       },       {            "source":"1",          "target":"5"       },       {            "source":"4",          "target":"5"       },       {            "source":"4",          "target":"6"       }    ] } 

my question being there means run string through algorithm show nodes , links joined. in example json should (after removal of redundant data) -

{      "fruits":[         {            "name":"apples",          "id":"1"       },       {            "name":"pears",          "id":"4"       },       {            "name":"grapes",          "id":"5"       }    ],    "links":[         {            "source":"1",          "target":"5"       },       {            "source":"4",          "target":"5"       }    ] }  

as grapes linked both apples , pears. have created algorithm remove duplicates works treat cannot crack 1 show linking entries.

i have iterated on target values

var lookup = {};                 var result = [];                 var links = jsonstring['links'];                 (var item, = 0; item = links[i++];) {                     var name = item.target;                      if (!(name in lookup)) {                         lookup[name] = 1;                          // remove unique values leave multiple occurances                          // based on source , target values removes none related fruits , rebuild json                      }                 } 

one way can think of is

  1. sort links array (for efficiency)
  2. iterate through links , search if adjacent items have same target
    1. if do, iterate through ones have same target , push links linksarray , fruits fruitsarray.
    2. if not, iterate till end , exit

in order fetch fruit iterate through data.fruits array , fruit based on source , target id

it this

function getlinkedfruits(data){    var sortedlinks = data.links.sort(function(a,b){     return a.target-b.target;   });    data.links = sortedlinks;    var resultlinks = [], resultfruits = [];    for(var i=0;i<data.links.length-1;i++){      if(data.links[i].target === data.links[i+1].target){        var j = i;        var fruit1 = getfruitsbasedonid(data.links[i].source);       var fruit2 = getfruitsbasedonid(data.links[i].target);        if(resultfruits.indexof(fruit1) == -1)         resultfruits.push(fruit1);        if(resultfruits.indexof(fruit2) == -1)         resultfruits.push(fruit2);        while(data.links[j].target == data.links[i].target){         resultlinks.push(data.links[j]);         var fruit3 = getfruitsbasedonid(data.links[j].source);         if(resultfruits.indexof(fruit3) == -1)           resultfruits.push(fruit3);         j++;       }        i=j-1;      }   }    function getfruitsbasedonid(id){     for(var i=0;i<data.fruits.length;i++){       if(data.fruits[i].id == id)         return data.fruits[i];     }   }    data.links = resultlinks;   data.fruits = resultfruits;    return data;  }  console.log(getlinkedfruits(data)); 

var data = {    "fruits": [{      "name": "apples",      "id": "1"    }, {      "name": "bananas",      "id": "2"    }, {      "name": "oranges",      "id": "3"    }, {      "name": "pears",      "id": "4"    }, {      "name": "grapes",      "id": "5"    }, {      "name": "strawberries",      "id": "6"    }],    "links": [{      "source": "1",      "target": "2"    }, {      "source": "3",      "target": "5"    }, {      "source": "1",      "target": "3"    }, {      "source": "5",      "target": "3"    }, {      "source": "1",      "target": "5"    }, {      "source": "4",      "target": "5"    }, {      "source": "4",      "target": "6"    }]  };    function getlinkedfruits(data) {      var sortedlinks = data.links.sort(function(a, b) {      return a.target - b.target;    });    data.links = sortedlinks;    var resultlinks = [],      resultfruits = [];    (var = 0; < data.links.length - 1; i++) {      if (data.links[i].target === data.links[i + 1].target) {        var j = i;        var fruit1 = getfruitsbasedonid(data.links[i].source);        var fruit2 = getfruitsbasedonid(data.links[i].target);        if (resultfruits.indexof(fruit1) == -1)          resultfruits.push(fruit1);        if (resultfruits.indexof(fruit2) == -1)          resultfruits.push(fruit2);        while (data.links[j].target == data.links[i].target) {          resultlinks.push(data.links[j]);          var fruit3 = getfruitsbasedonid(data.links[j].source);          if (resultfruits.indexof(fruit3) == -1)            resultfruits.push(fruit3);          j++;        }        = j - 1;      }    }      function getfruitsbasedonid(id) {      (var = 0; < data.fruits.length; i++) {        if (data.fruits[i].id == id)          return data.fruits[i];      }    }      data.links = resultlinks;    data.fruits = resultfruits;      return data;    }    console.log(getlinkedfruits(data));


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -