Skip to content

Instantly share code, notes, and snippets.

@pawarvijay
Created April 7, 2017 05:50
Show Gist options
  • Save pawarvijay/9f81b0a8ff740f281d30c68e0c2541db to your computer and use it in GitHub Desktop.
Save pawarvijay/9f81b0a8ff740f281d30c68e0c2541db to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/wezeri
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var data = {
name : 'vijay',
friends : [
{
name:'manish',
friends : [
{name :"ajit", friends : []}
]
},
{
name : 'pranit',
friends : [
{name : 'prajakta', friends : []},
{name : 'pavan', friends : []}
]
},
{
name : 'rahul',
friends : [
{
name : 'parul',
friends : [
{
name : 'tom' ,
friends : []
}
]
}
]
}
]
}
var currentNode = data;
var visitedList = []
var q = []
function exploreCurrentNode(){
currentNode.friends.forEach(function(friend){
var object = Object.assign({}, friend);
object.daddy = currentNode.name;
visitedList.push(object)
q.push(object)
})
}
exploreCurrentNode()
while (q.length > 0) {
emptyQueue()
}
function emptyQueue(){
q.forEach(function(itemq){
currentNode = q.shift();
exploreCurrentNode()
})
}
visitedList.forEach(function(item){
console.log('daddy : ' + item.daddy + ' -----> child : ' + item.name)
})
console.log(q)
</script>
<script id="jsbin-source-javascript" type="text/javascript">var data = {
name : 'vijay',
friends : [
{
name:'manish',
friends : [
{name :"ajit", friends : []}
]
},
{
name : 'pranit',
friends : [
{name : 'prajakta', friends : []},
{name : 'pavan', friends : []}
]
},
{
name : 'rahul',
friends : [
{
name : 'parul',
friends : [
{
name : 'tom' ,
friends : []
}
]
}
]
}
]
}
var currentNode = data;
var visitedList = []
var q = []
function exploreCurrentNode(){
currentNode.friends.forEach(function(friend){
var object = Object.assign({}, friend);
object.daddy = currentNode.name;
visitedList.push(object)
q.push(object)
})
}
exploreCurrentNode()
while (q.length > 0) {
emptyQueue()
}
function emptyQueue(){
q.forEach(function(itemq){
currentNode = q.shift();
exploreCurrentNode()
})
}
visitedList.forEach(function(item){
console.log('daddy : ' + item.daddy + ' -----> child : ' + item.name)
})
console.log(q)
</script></body>
</html>
var data = {
name : 'vijay',
friends : [
{
name:'manish',
friends : [
{name :"ajit", friends : []}
]
},
{
name : 'pranit',
friends : [
{name : 'prajakta', friends : []},
{name : 'pavan', friends : []}
]
},
{
name : 'rahul',
friends : [
{
name : 'parul',
friends : [
{
name : 'tom' ,
friends : []
}
]
}
]
}
]
}
var currentNode = data;
var visitedList = []
var q = []
function exploreCurrentNode(){
currentNode.friends.forEach(function(friend){
var object = Object.assign({}, friend);
object.daddy = currentNode.name;
visitedList.push(object)
q.push(object)
})
}
exploreCurrentNode()
while (q.length > 0) {
emptyQueue()
}
function emptyQueue(){
q.forEach(function(itemq){
currentNode = q.shift();
exploreCurrentNode()
})
}
visitedList.forEach(function(item){
console.log('daddy : ' + item.daddy + ' -----> child : ' + item.name)
})
console.log(q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment