I am creating JsTree with json Data. the tree is created upto second level but after second the tree is not populated.
I want to recursively call the function to add leaf to it until leaf doesnt exit.
Below is my Json:
[ {
"id" : 25,
"name" : "Output",
"path" : "/",
"type" : "folder",
}, {
"id" : 26,
"name" : "Templates",
"path" : "/",
"type" : "folder",
}, {
"id" : 27,
"name" : "Temp",
"path" : "/Output/",
"type" : "folder",
}, {
"id" : 28,
"name" : "December",
"path" : "/Output/",
"type" : "folder",
}, {
"id" : 29,
"name" : "ParameterDOC",
"path" : "/Templates/DecemberTemplates/",
"type" : "Doc",
}, {
"id" : 30,
"name" : "SimpleDoc",
"path" : "/Templates/DecemberTemplates/",
"type" : "Doc",
}, {
"id" : 31,
"name" : "DecemberTemplates",
"path" : "/Templates/",
"type" : "folder",
}, {
"id" : 32,
"name" : "NovemberTemplates",
"path" : "/Templates/",
"type" : "folder",
} ]
and code is:
function createTreeForDoc(jsonData,path,id){
for(var i=0; i < jsonData.length;i++){
if(jsonData[i].path == path){
$("<li id="+jsonData[i].name+" class=\"lib\" rel="+jsonData[i].path+" data-jstree='{\"icon\":\"images/"+jsonData[i].type+"_small.png\"}' >"+jsonData[i].name+"</li>").appendTo("#maptree");
createSubTree(jsonData,jsonData[i].path+jsonData[i].name+"/",jsonData[i].name,i);
}
}
$('#rptTree').jstree({
"plugins" : ["types"],
"core" : { "check_callback" : false, "themes" : { "dots" : false }, },
});
}
function createSubTree(jsonData,path,gid,counter){
$("<ul id=\"test"+counter+"\"></ul>").appendTo("#"+gid);
for(var i=0; i < jsonData.length;i++){
if(jsonData[i].path == path ){
$("<li id="+jsonData[i].name+" class=\"lib\" rel="+jsonData[i].path+" >"+jsonData[i].name+"</li>").appendTo("#test"+counter);
}
}
}
and right now i am getting out this:
so this creates till second level third level is not created how to recursively call a function to create all other level to end.
I am not able to get what you are trying to do ,but by looking your json data you can do in the following way.Here I am using a recursive function:
function filterData(globalData,parentPath){
var result=[];
for(var i =0; i<globalData.length;i++){
if(globalData[i].path == parentPath){
result.push(data[i]);
}
}
return result;
}
function renderTree(data,parentNodeId)
{
for (var i=0; i<data.length;i++)
{
if(data[i].type == 'folder')
{
$("<li id="+data[i].name+ ">"+data[i].name+"</li>").appendTo($('#'+parentNodeId));
var childNodes=filterData(globalData,data[i].path+data[i].name+"/");
if(childNodes.length > 0)
{
$("<ul id="+data[i].path+data[i].name+"></ul>").appendTo($('#'+data[i].name));
renderTree(childNodes,data[i].path+data[i].name);
}
}
else
{
$("<li id="+data[i].name+">"+data[i].name+"</li>").appendTo($('#'+parentNodeId));
}
}
hope it will work.
You have to use a recursive function to build your tree. This function should look like:
function recursive(jsonData,parent,counter=0)
{
$("<li id="+parent.path+parent.name+" class=\"lib\" rel="+parent.path+" >"+parent.name+"</li>").appendTo("#test"+counter);
counter = 0;
for(a = 0; a<jsonData.length; a++)
{
if((parent.path+ parent.name) == jsonData[a].path)
{
$("<li id="+jsonData[a].path+jsonData[a].name+" class=\"lib\" rel="+jsonData[a].path+" data-jstree='{\"icon\":\"images/"+jsonData[i].type+"_small.png\"}' >"+jsonData[a].name+"</li>").appendTo("#maptree");
if(jsonData[a].type == "folder")
{
recursive(jsonData,jsonData[a],counter);
}
}
counter++;
}
}
This is an example, i'm notsure it works but it might be close enought for you to enhance it. :)
Good luck...
Related
I have a Firebase realtime-database export-json that has nested informations, that I would like to extract. The structure of the JSON looks like this:
{
"users" : {
"024w97mv8NftGFY8THfQIU6PhaJ3" : {
"email" : "xxx",
"id" : "024w97mv8NftGFY8THfQIU6PhaJ3",
"name" : "xxx",
"items" : {
"-LQL9n-r9BGdo3HJZ2sk" : {
"disliked" : true,
"id" : 396,
"name" : "Aaa"
},
"-LQL9oO63nH-QW2w6zz0" : {
"liked" : true,
"id" : 3674,
"name" : "Bbb"
}
}
},
"0ERLT5DLRvbZUnjlnM7Ow0qItpz2" : {
"email" : "zzz",
"id" : "0ERLT5DLRvbZUnjlnM7Ow0qItpz2",
"name" : "zzz",
"items" : {
"-LIZnriSVQMzqTsPFNYa" : {
"id" : 396,
"liked" : true,
"name" : "Aaa"
},
"-LIZnrzOuk4WyjqEiLG8" : {
"disliked" : true,
"id" : 4805,
"name" : "Ccc"
}
}
}
}
}
What I need to achieve is getting a list of all liked item-names, and ideally counting how often an item is liked.
Is there a simple tool or script to do that? Ruby or Javascript would be preferred. Thanks a lot!
You can parse your JSON data in ruby like below
result_hash = JSON.parse(result)
result_ary = result_hash["users"].collect do |k,v|
v["items"].values.select{|v1| v1["liked"] == true }
end
result_data = result_ary.flatten
result of parsing
=> [{"liked"=>true, "id"=>3674, "name"=>"Bbb"}, {"id"=>396, "liked"=>true, "name"=>"Aaa"}]
Now its very easy for getting your required result
result_data.collect{|x| x["name"] }
=> ["Bbb", "Aaa"]
result_data.count {|x| x["name"] == "Aaa"}
=> 1
result_data.count {|x| x["name"] == "Bbb"}
=> 1
Declaration of id
var id = 3;
I want to update this object
var obj = {"comments" : {
"commentedBy" : "test",
"date" : "",
"comment" : "Hello world",
"subComments" : {
"commentedBy" : "jaril 2",
"date" : "",
"comment" : "Hello world inside dark",
"subComments" : {
"commentedBy" : "jaril 3",
"date" : "",
"comment" : "wow working great",
"subComments" : {
"commentedBy" : "jaril 4",
"date" : "",
"comment" : "wow working great",
"commentId" : 4
},
"commentId" : 3
},
"commentId" : 2
},
"commentId" : 1
},
"dueDate" : "",
"createdDate" : "",
"lastUpdated" : "",
"checkList" : [],
"position" : 2,
"status" : "active"
}
}
Function is this
function deleteCommentId(comments){
if (comments.commentId == id)){
delete comments;
return comments;
}
if (comments.subComments) {
deleteCommentId(comments.subComments);
}
return comments;
}
Function Object is this
if(id == 1){
result[0].comments = {};
} else {
deleteCommentId(obj.comments);
}
console.log("final object==>", obj);
I want output like this
{"comments" : {
"commentedBy" : "test",
"date" : "",
"comment" : "Hello world",
"subComments" : {
"commentedBy" : "jaril 2",
"date" : "",
"comment" : "Hello world inside dark",
"commentId" : 2
},
"commentId" : 1
},
"dueDate" : "",
"createdDate" : "",
"lastUpdated" : "",
"checkList" : [],
"position" : 2,
"status" : "active"
}
}
Any help would be appreciated
Note: I want to remove nested subcomments object using id if I pass
id=3 then it should remove subcomment of 2, How can I remove subcomments of 2 if id = 3
The function below should do it.
(I removed the irrelevant parts or your object to make things a bit easier to read.)
var obj = {
"comments": {
"subComments": {
"subComments": {
"subComments": {
"commentId": 4
},
"commentId": 3
},
"commentId": 2
},
"commentId": 1
}
};
function deleteCommentId(comments, id) {
if (comments.subComments) {
if (comments.subComments.commentId === id) {
delete comments.subComments;
} else {
deleteCommentId(comments.subComments, id);
}
}
}
deleteCommentId(obj.comments, 3);
console.log(obj);
var id = 3;
var obj = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","subComments":{"commentedBy":"jaril 3","date":"","comment":"wow working great","subComments":{"commentedBy":"jaril 4","date":"","comment":"wow working great","commentId":4},"commentId":3},"commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"}
function deleteCommentId(comments, id) {
if (comments.subComments) {
if (comments.subComments.commentId === id) {
delete comments.subComments;
} else {
deleteCommentId(comments.subComments, id);
}
}
}
deleteCommentId(obj.comments, id);
console.log("final object==>",obj);
var expectedJSON = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"}
console.log("Output match: ",JSON.stringify(obj) == JSON.stringify(expectedJSON));
I have two collections
1) Floorplan_backup.
The structure of the document looks like :-
{
"_id" : ObjectId("5877e18a88db272b578572bd"),
"floorplans" : [
{
"name" : "Campus Center - Atrium",
"uuid" : NumberLong(3),
"cameras" : []
},
{
"name" : "Campus Center - Bridge ",
"uuid" : NumberLong(4),
"cameras" : []
},
{
"name" : "Campus Center - Top",
"uuid" : NumberLong(5),
"cameras" : []
},
{
"name" : "Performance Dining Hall",
"uuid" : NumberLong(6),
"cameras" : []
},
{
"name" : "Main Kitchen",
"uuid" : NumberLong(7),
"cameras" : []
},
{
"name" : "OrgChart - Board of Trustees",
"uuid" : NumberLong(8),
"cameras" : []
},
{
"name" : "OrgChart - Academic Affairs",
"uuid" : NumberLong(9),
"cameras" : []
},
{
"name" : "OrgChart - Admissions",
"uuid" : NumberLong(10),
"cameras" : []
},
{
"name" : "OrgChart - Athletics",
"uuid" : NumberLong(11),
"cameras" : []
},
{
"name" : "OrgChart - Financial",
"uuid" : NumberLong(14),
"cameras" : []
},
{
"name" : "OrgChart - HR",
"uuid" : NumberLong(15),
"cameras" : []
},
{
"name" : "OrgChart - Institutional",
"uuid" : NumberLong(16),
"cameras" : []
},
{
"name" : "OrgChart - IT",
"uuid" : NumberLong(17),
"cameras" : []
},
{
"name" : "OrgChart - Student Success",
"uuid" : NumberLong(20),
"cameras" : []
},
{
"name" : "OrgChart - Student Life",
"uuid" : NumberLong(21),
"cameras" : []
}
],
"userid" : "user-56cb3c4c0c953470865336",
"firstviewhost" : undefined
}
2) Floorplan . The structure of the document
{
"_id" : ObjectId("589025b03422cafc09913363"),
"userid" : "user-56cb3c4c0c953470865336",
"firstviewhost" : "dean.vizsafe.com"
}
I would like to update the firstviewhost in Floorpla_backup with the value "dean.vizsafe.com" by matching the userid of both the collections.
The javascript code used is :-
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.find({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});
But is is not giving me the desired resultset . Could you let me know what is wrong with this?
doc2 is an array. Use findOne to get an object.
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});
find returns you a cursor, instead use findOne.
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});
I am trying to get only the ObjectId's from One specific Document that is embedded in the projects Array.
Basically I am trying to make a database that will have users and each user will have there own projects.
Thank you !
db.users.find().pretty()
{
"_id" : ObjectId("5762c0cf2b9a78006373a684"),
"name" : "seq",
"pass" : "seq",
"projects" : [
{
"pid" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},
{
"pid" : ObjectId("5762c0ba2b9a78006373a683"),
"name" : "bbb"
}
]
}
{
"_id" : ObjectId("5762c28d2b9a78006373a687"),
"name" : "leq",
"pass" : "leq",
"projects" : [
{
"pid" : ObjectId("5762c2892b9a78006373a685"),
"name" : "ccc"
},
{
"pid" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
]
}
let say we want two pids
{"pid" : ObjectId("5762c0ba2b9a78006373a682")} and
{"pid" : ObjectId("5762c2892b9a78006373a686"),}
and only inner documents
so required response should look like:
{
"_id" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},{
"_id" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
Aggregation framework can manipulate documents, match only needed ones and transform inner structure by project phase:
var match = {
$match : {
"projects.pid" : {
$in : [ObjectId("5762c0ba2b9a78006373a682"),
ObjectId("5762c2892b9a78006373a686")]
}
}
}
var unwind = {
$unwind : "$projects"
};
// now move array objet as top level object
var project = {
$project : {
_id : "$projects.pid",
name : "$projects.name",
// list other fields here
}
}
db.vic.aggregate([match, unwind, match, project])
I am trying to delete "virtualNumber" : "12345" in the following document:
{
"_id" : ObjectId("50a9db5bdc7a04df06000005"),
"billingInfo" : null,
"date" : "dsfdsfsdfsd",
"description" : "sdfsdff",
"pbx" : {
"_id" : ObjectId("50a9db5bdc7a04df06000006"),
"did" : {
"1234567890" : {
"inventoryId" : "509df7547e84b25e18000001",
"didcountry" : "india",
"didState" : "bangalore",
"routeType" : "CallForward",
"didNumber" : "1234567890",
"didVirtualNumbers" : [
{
"virtualNumber" : "12345"
},
{
"virtualNumber" : "56789"
}
],
"id" : ObjectId("50a9db9acdfb4f9217000002")
}
},
},
I am using node.js, so I constructed a query in JavaScript:
var query = {_id: ObjectId("50a9db5bdc7a04df06000005")};
var obj = {};
obj["pbx.did.1234567890.didVirtualNumbers.virtualNumber"]=12345;
//problem
collection.update(query,{$pull:obj});
You need to match the array element like:
{"$pull": {"pbx.did.7259591220.didVirtualNumbers": {"virtualNumber": "12345"}}}
So you should change your code to:
obj["pbx.did.7259591220.didVirtualNumbers"]={"virtualNumber": "12345"};
Please refer to http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull
It mentions the pull field should be an array.