General Tree Post Order Traversal - javascript

var tree = {
"name" : "root",
"children" : [
{
"name" : "first child",
"children" : [
{
"name" : "first child of first",
"children" : []
},
{
"name" : "second child of first",
"children" : []
}
]
},
{
"name" : "second child",
"children" : []
}
]
}
function postOrder(root) {
if (root == null) return;
postOrder(root.children[0]);
postOrder(root.children[1]);
console.log(root.name);
}
postOrder(tree);
Heres my code for a recursive post order traversal in javascript using a JSON tree.
How would I go about adapting this code to handle N children in a node?

This should do what you want: just replace your calls to postOrder with root.children.forEach(postOrder);.
var tree = {
"name" : "root",
"children" : [
{
"name" : "first child",
"children" : [
{
"name" : "first child of first",
"children" : []
},
{
"name" : "second child of first",
"children" : []
}
]
},
{
"name" : "second child",
"children" : []
}
]
}
function postOrder(root) {
if (root == null) return;
root.children.forEach(postOrder);
console.log(root.name);
}
postOrder(tree);
I'd also move the line that prints the root name before the call that prints the children names recursively, but this may not match your use case.

Related

How to get the clicked object in jstree

Wanted to get the clicked tree object value in javascript
what i'm trying to achieve is when user clicks a tree node then its related data i want to get the original object
here is what i have tried not getting object:
$('#jstree').jstree({
"json_data" : {
"data" : [
{
"data" : "A node",
"metadata" : { id : 23 },
"children" : [ "Child 1", "A Child 2" ]
},
{
"attr" : { "id" : "li.node.id1" },
"data" : {
"title" : "Long format demo",
"attr" : { "href" : "#" }
}
}
]
},
"plugins" : [ "themes", "json_data", "ui" ]
});
$("#jstree").bind(
"select_node.jstree", function(evt, data){
//selected node object: data.node;
console.log('data',data.inst.get_json());
console.log('data.node.id',data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://old.static.jstree.com/v.1.0pre/jquery.jstree.js"></script>
<div id="jstree">
</div>
Expected Output:
from above snippet, when i click Child 1 or A Child 2 i should get below object
{
"data" : "A node",
"metadata" : { id : 23 },
"children" : [ "Child 1", "A Child 2" ]
}
when user clicks on Long format demo i should get below object
{
"attr" : { "id" : "li.node.id1" },
"data" : {
"title" : "Long format demo",
"attr" : { "href" : "#" }
}
}
Please help me thanks in advance!!!
As your jstree version is old, I recommend you to update your jstree version. but, as for the 1.0v, You can use below code to get the parent node, if user clicked child one.
$('#jstree').jstree({
"json_data" : {
"data" : [
{
"data" : "A node",
"metadata" : { id : 23 },
"children" : [ "Child 1", "A Child 2" ]
},
{
"attr" : { "id" : "li.node.id1" },
"data" : {
"title" : "Long format demo",
"attr" : { "href" : "#" }
}
}
]
},
"plugins" : [ "themes", "json_data", "ui" ]
});
$("#jstree").bind(
"select_node.jstree", function(evt, data){
if(data.inst._get_parent().length <= 0 || data.inst._get_parent()===-1){
console.log('data',data.inst.get_json());
console.log('data.node.id',data);
}else{
var parent = data.inst._get_parent();
console.log(data.inst.get_json(parent));
}
//selected node object: data.node;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://old.static.jstree.com/v.1.0pre/jquery.jstree.js"></script>
<div id="jstree">
</div>

Get closest locations in an Array

I have a simple document, which has 3 location objects in an array.
Data:
{
"_id" : ObjectId("57c3c479a306b3613cf1ee5b"),
"location_history" : [
{
"location_name" : "Area 1",
"date" : 1472447609,
"_id" : ObjectId("57c3c479ac5a69612f0e0899"),
"location" : [
24.9532107, 67.1790576
]
},
{
"location_name" : "Area 2",
"date" : 1472448059,
"_id" : ObjectId("57c3c63bac5a69612f0e089c"),
"location" : [
24.9663937, 67.1462044
]
},
{
"location_name" : "Area 3",
"date" : 1472448987,
"_id" : ObjectId("57c3c9dbac5a69612f0e08a0"),
"location" : [
-24.987325, 115.1862298
]
}
}
Question: I need to fetch closest locations in this array.
Query I have tried:
db.getCollection('consumers_locations').aggregate([
{"$unwind": "$location_history"},
{"$match":{"_id":ObjectId("57c3c479a306b3613cf1ee5b")}},
{"$project" : { "abc" : "$location_history.location"} },
{ $geoNear: {
near: { type: "Point", coordinates: [ 24.942785, 67.157855 ] },
distanceField: "distance",
query : {"_id" : "_id"},
uniqueDocs: true,
includeLocs: "search_history.location",
maxDistance : 10000
}
}
])
But I get an error:
"ok" : 0,
"errmsg" : "$geoNear is only valid as the first stage in a pipeline.",
"code" : 2,
"codeName" : "BadValue"
Expected Output:
{
"_id" : ObjectId("57c3c479a306b3613cf1ee5b"),
"location_history" : [
{
"location_name" : "Area 1",
"date" : 1472447609,
"_id" : ObjectId("57c3c479ac5a69612f0e0899"),
"location" : [
24.9532107, 67.1790576
]
},
{
"location_name" : "Area 2",
"date" : 1472448059,
"_id" : ObjectId("57c3c63bac5a69612f0e089c"),
"location" : [
24.9663937, 67.1462044
]
}
}
It is not doable with your schema. Indexes are used to order documents in a collection, not sorting subdocuments within a document.
Consider to create a separate location_history collection with references to the parent document in consumers_locations. E.g. for your object, the collection may look like:
db.getCollection('location_history').insert([
{
"consumer_location": ObjectId("57c3c479a306b3613cf1ee5b"),
"location_name" : "Area 1",
"date" : 1472447609,
"_id" : ObjectId("57c3c479ac5a69612f0e0899"),
"location" : [
24.9532107, 67.1790576
]
},
{
"consumer_location": ObjectId("57c3c479a306b3613cf1ee5b"),
"location_name" : "Area 2",
"date" : 1472448059,
"_id" : ObjectId("57c3c63bac5a69612f0e089c"),
"location" : [
24.9663937, 67.1462044
]
},
{
"consumer_location": ObjectId("57c3c479a306b3613cf1ee5b"),
"location_name" : "Area 3",
"date" : 1472448987,
"_id" : ObjectId("57c3c9dbac5a69612f0e08a0"),
"location" : [
-24.987325, 115.1862298
]
}
]);
Regarding to the error, the docs read:
You can only use $geoNear as the first stage of a pipeline.
since only the first stage can benefit from indexes.

MongoDB: Getting document and only one object of an array

I got multiple documents like this:
{
"_id" : "sgG6G9XTvvjj7uxwQ",
"title" : "A title",
"notes" : [
{
"id" : "ePce6fBAHx9KeKjuM",
"timestamp" : 1453731403807,
"message" : "some text",
"name" : "Tom"
},
{
"id" : "BAHx9KeKjuMePce6f",
"timestamp" : 1453731403808,
"message" : "some text",
"name" : "Bob"
},
{
"id" : "ePce6fBAHx9KeKjuM",
"timestamp" : 1453731403809,
"message" : "some text",
"name" : "Tom"
}
]
}
I get this document by using this find-query:
Collection.find({}, { sort: { title: 1 }});
But I don't need the complete notes field. I'm only interested in the id of the last note-object. That means I have to order all notes object by timestamp and just take the first (=newest) one.
I'm not quite sure, if I can do that by the find-query or if I have to do that after getting the complete data.
So the best result for me would be:
{
"_id" : "sgG6G9XTvvjj7uxwQ",
"title" : "A title",
"notes" : [
{
"id" : "ePce6fBAHx9KeKjuM"
}
]
}
You can use the $slice projection to limit the number of array elements returned. For example:
db.collection.find({}, {title: 1, "notes.id": 1, notes: {$slice: 1}}).sort({title: 1});
Will return:
{
"_id" : "sgG6G9XTvvjj7uxwQ",
"title" : "A title",
"notes" : [
{
"id" : "ePce6fBAHx9KeKjuM"
}
]
}

How to go through JSON data to create a list, even the children?

I have a JSON object that I need to create a list out of.
I would be able to do it fine but each object can have children. It looks something like this :
{
"Boys" :
[
{
"name" : "Fred",
"age" : "65",
"children" : [{
"name" : "dave",
"age" : "24",
"children" : []
}, {
"name" : "cliff",
"age" : "32",
"children" : []
}
]
},
{
"name" : "jon",
"age" : "46",
"children" : [{
"name" : "jess",
"age" : "26",
"children" : []
}, {
"name" : "gloria",
"age" : "19",
"children" : []
}
]
}
],
"Girls" :
[
{
"name" : "Jane",
"age" : "65",
"children" : [{
"name" : "dave",
"age" : "24",
"children" : []
}, {
"name" : "grace",
"age" : "32",
"children" : []
}
]
},
{
"name" : "ariana",
"age" : "46",
"children" : [{
"name" : "jessy",
"age" : "28",
"children" : []
}, {
"name" : "niki",
"age" : "19",
"children" : []
}
]
}
]
}
I'd be able to go through it fine in a for-loop but I'm unsure how to go through the children too.
Basically I want to go through each element and create a list item with the text being the name of the object I am currently at.
So for the JSON above it would be something like :
-Boys
-Fred
-Dave
-Cliff
-Jon
-Jess
-Gloria
-Girls
-Jane
-Dave
-Grace
-Ariana
-Jessy
-Niki
After I create this list I will be using JSTree to format it, any help is appreciated :)
You need a recursive function, a function that calls itself until something happens. In your case, you need a function that goes through a list of people. For each person, it'd paint the name, then would check if that person has children. If it does, then calls the function again, passing it the list of children.
Something like this:
var renderList = function(list){
var $list = $('<ul>');
$.each(list, function(i, element){
var $child = $('<li>'+element.name+'</li>');
if (element.children.length > 0) {
$child.append(renderList(element.children));
}
$list.append($child);
});
return $list;
};
$('#boys').append(renderList(data.Boys)); // Start list of boys
$('#girls').append(renderList(data.Girls)); // Start list of girls
Working example here http://jsfiddle.net/zbm778ag/

JSON Object array inside array find and replace in javascript

I have one JSON Object like this :
var myObject = [
{
"Name" : "app1",
"id" : "1",
"groups" : [
{ "id" : "test1",
"name" : "test group 1",
"desc" : "this is a test group"
},
{ "id" : "test2",
"name" : "test group 2",
"desc" : "this is another test group"
}
]
},
{
"Name" : "app2",
"id" : "2",
"groups" : [
{ "id" : "test3",
"name" : "test group 4",
"desc" : "this is a test group"
},
{ "id" : "test4",
"name" : "test group 4",
"desc" : "this is another test group"
}
]
},
{
"Name" : "app3",
"id" : "3",
"groups" : [
{ "id" : "test5",
"name" : "test group 5",
"desc" : "this is a test group"
},
{ "id" : "test6",
"name" : "test group 6",
"desc" : "this is another test group"
}
]
}
];
I have new value available of "name" for specific "id".
How can I replace "name" of specific "id" inside any object ?
And how to count total number of groups among all objects ?
for example : replace name to "test grp45" for id = "test1"
Here is fiddle
http://jsfiddle.net/qLTB7/21/
The following function will search through an object and all of its child objects/arrays, and replace the key with the new value. It will apply globally, so it won't stop after the first replacement. Uncomment the commented line to make it that way.
function findAndReplace(object, value, replacevalue) {
for (var x in object) {
if (object.hasOwnProperty(x)) {
if (typeof object[x] == 'object') {
findAndReplace(object[x], value, replacevalue);
}
if (object[x] == value) {
object["name"] = replacevalue;
// break; // uncomment to stop after first replacement
}
}
}
}
Working jsfiddle: http://jsfiddle.net/qLTB7/28/
Try this
function findAndReplace(object,keyvalue, name) {
object.map(function (a) {
if (a.groups[0].id == keyvalue) {
a.groups[0].name = name
}
})
}
findAndReplace(myObject,"test1" ,"test grp45");
Here's a different approach using Array.prototype.some. It assumes that the Name property in the outer objects should be actually be name (note capitalisation).
function updateNameById(obj, id, value) {
Object.keys(obj).some(function(key) {
if (obj[key].id == id) {
obj[key].name = value;
return true; // Stops looping
}
// Recurse over lower objects
else if (obj[key].groups) {
return updateNameById(obj[key].groups, id, value);
}
})
}
The advantage of some is that it stops as soon as the callback returns true.
I think this should work for you:-
var id = 'test1';
var newname = 'test grp45';
var numberOfGruops = 0;
myObject.forEach(function(app){
numberOfGruops += app.groups.length; //Count all groups in this app
app.groups.forEach(function(group){
if(group.id===id)
group.name = newname; // replace the name
});
});
Maybe a more succinct sol'n
function changeName(objArray, objId, newName) {
objArray.forEach(function(obj) {
if (obj.id === objId) obj.Name = newName;
});
}
Personally: if this were me, when creating these objects, I would create a new obj and key them by id.
var myApps = {};
myObject.forEach(function(o) {
myApps[o.id] = o;
});
=>
{
"1": {
"Name": "app1",
"id": "1",
"groups": [
{
"id": "test1",
"name": "test group 1",
"desc": "this is a test group"
},
{
"id": "test2",
"name": "test group 2",
"desc": "this is another test group"
}
]
}
}
And then you could just do:
myApps['someId'].name = 'This is my new Name'
Check it out here:
http://jsfiddle.net/qLTB7/40/
it should be if (object["id"] == value) instead of if (object[x] == value) in 7th line of PitaJ answer, so whole function will look like:
function findAndReplace(object, value, replacevalue) {
for (var x in object) {
if (object.hasOwnProperty(x)) {
if (typeof object[x] == 'object') {
findAndReplace(object[x], value, replacevalue);
}
if (object["id"] == value) {
object["name"] = replacevalue;
// break; // uncomment to stop after first replacement
}
}
}
}
if you leave object[x] - function will replace name also for objects with other keys values set to "test1", for example
{"id": "xxx", "name": "test group 1", "desc": "test1"}

Categories

Resources