underscore - locating node based in a value - javascript

This is my 1st time using underscore...I've this simple json...
"categories" : [
{
"tag" : "cat1",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1",
"pt" : "Categoria 1"
},
"children" : [
{
"tag" : "cat11",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1.1",
"pt" : "Categoria 1.1"
}
},
{
"tag" : "cat12",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 1.2",
"pt" : "Categoria 1.2"
}
}
]
},
{
"tag" : "cat2",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2",
"pt" : "Categoria 2"
},
"children" : [
{
"tag" : "cat21",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2.1",
"pt" : "Categoria 2.1"
}
},
{
"tag" : "cat22",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 2.2",
"pt" : "Categoria 2.2"
}
}
]
},
{
"tag" : "cat3",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3",
"pt" : "Categoria 3"
},
"children" : [
{
"tag" : "cat31",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3.1",
"pt" : "Categoria 3.1"
}
},
{
"tag" : "cat32",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 3.2",
"pt" : "Categoria 3.2"
}
}
]
},
{
"tag" : "cat4",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4",
"pt" : "Categoria 4"
},
"children" : [
{
"tag" : "cat41",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4.1",
"pt" : "Categoria 4.1"
}
},
{
"tag" : "cat42",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 4.2",
"pt" : "Categoria 4.2"
}
}
]
}
]
you can see I've the TAG key in several places. I need to get the entire tree based in a criteria for tag. I was using, find, filter, where and findWhere and all the time I got the same results:
var find = _.find($rootScope.webshop.categories, {tag: 'cat1'});
this works !
but if I try...
var find = _.find($rootScope.webshop.categories, {tag: 'cat11'});
No results :( Even using _.where or ._filter or ._findWhere. - The results always will be the same.
Can someone help a beginner in Underscore with something that's probably simple ?!
ty !

_.find does not recurse, you will have to build your own solution. Something like this:
_.findIn = function() {
var args = Array.prototype.slice.call(arguments, 0);
var childrenProp = args[0];
var result = _.find.apply(_, args.slice(1));
if (result !== void(8)) return result;
var arr = args[1];
for (var i = 0, l = arr.length; i < l; i++) {
args[1] = arr[i][childrenProp];
var result = _.findIn.apply(_, args);
if (result !== void(8)) return result;
}
return void(8);
}
_.findIn('children', categories, {tag: 'cat11'})
categories = [
{
"tag" : "cat1",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1",
"pt" : "Categoria 1"
},
"children" : [
{
"tag" : "cat11",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1.1",
"pt" : "Categoria 1.1"
}
},
{
"tag" : "cat12",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 1.2",
"pt" : "Categoria 1.2"
}
}
]
},
{
"tag" : "cat2",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2",
"pt" : "Categoria 2"
},
"children" : [
{
"tag" : "cat21",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2.1",
"pt" : "Categoria 2.1"
}
},
{
"tag" : "cat22",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 2.2",
"pt" : "Categoria 2.2"
}
}
]
},
{
"tag" : "cat3",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3",
"pt" : "Categoria 3"
},
"children" : [
{
"tag" : "cat31",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3.1",
"pt" : "Categoria 3.1"
}
},
{
"tag" : "cat32",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 3.2",
"pt" : "Categoria 3.2"
}
}
]
},
{
"tag" : "cat4",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4",
"pt" : "Categoria 4"
},
"children" : [
{
"tag" : "cat41",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4.1",
"pt" : "Categoria 4.1"
}
},
{
"tag" : "cat42",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 4.2",
"pt" : "Categoria 4.2"
}
}
]
}
]
_.findIn = function() {
var args = Array.prototype.slice.call(arguments, 0);
var childrenProp = args[0];
var result = _.find.apply(_, args.slice(1));
if (result !== void(8)) return result;
var arr = args[1];
for (var i = 0, l = arr.length; i < l; i++) {
args[1] = arr[i][childrenProp];
var result = _.findIn.apply(_, args);
if (result !== void(8)) return result;
}
return void(8);
}
document.write(JSON.stringify(_.findIn('children', categories, {tag: 'cat11'})));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

Related

Complex mongodb data how to update

I'm trying to update show_seats.showByDate.shows.showSeats.seat_details.values.seat_status
this my code .........................................................................................................................................................
db.seats.updateOne({'show_seats.showByDate.shows.showSeats.seat_details.values._id':"62a9e044ec028e60180fe28a"},{$set:{'show_seats.showByDate.shows.showSeats.seat_details.values.seat_status' : true}})
.........................................................................................................................................................
please say what did i wrong**
{
"_id" : ObjectId("62a43ac2d7213c7233cd1dee"),
"totalShowByDay" : "2",
"totalShowDays" : 4,
"movieId" : ObjectId("62953ba3cb6ae625ec9433e6"),
"screenId" : ObjectId("6293b9638dde2d92658d5513"),
"createdAt" : 1654930114438,
"showId" : ObjectId("62a43ac2d7213c7233cd14ed"),
"show_seats" : [
{
"showByDate" : {
"ShowDate" : "2022-06-11",
"shows" : [
{
"showTime" : "2022-06-11T10:00",
"showSeats" : [
[
{
"category" : "CLASSIC",
"seat_details" : [
{
"key" : "A",
"values" : [
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ee"),
"seat_number" : "1",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
,
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ef"),
"seat_number" : "2",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
{
"_id" : ObjectId("62a43ac2d7213c7233cd14f0"),
"seat_number" : "3",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00",,
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ef"),
"seat_number" : "2",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
{
"_id" : ObjectId("62a43ac2d7213c7233cd14f0"),
"seat_number" : "3",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
}
this is what i ended up doing, I need best solution
await db.getDb().collection(coll.seat).aggregate([
{
'$unwind': {
'path': '$show_seats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats.seat_details'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats.seat_details.values'
}
}, {
'$match': {
'show_seats.showByDate.shows.showSeats.seat_details.values._id': ObjectId("62a43ac2d7213c7233cd14ee")
}
}, {
$addFields: { "show_seats.showByDate.shows.showSeats.seat_details.values.seat_status": true }
}
])

Create Functionality in Context Menu of jsTree not working

New nodes won't be created when 'types' plugin is defined.
Please look at this fiddle.
I'm unable to create new Nodes in the tree.
http://jsfiddle.net/z8L5r9w3/1/
$('#jstree').jstree({
"core" : {
"check_callback" : true,
"data" : [
{ "text" : "Branch 1", "type" : "branch", "children" : [
{ "text" : "leaf 1.1", "type" : "leaf" },
{ "text" : "leaf 1.2", "type" : "leaf" },
{ "text" : "leaf 1.3", "type" : "leaf" }
]
},
{ "text" : "Branch 2", "type" : "branch", "children" : [
{ "text" : "leaf 2.1", "type" : "leaf" },
{ "text" : "leaf 2.2", "type" : "leaf" },
{ "text" : "leaf 2.3", "type" : "leaf" }
]
}
]
},
"types" : {
"#" : {
"valid_children" : ["branch"]
},
"branch" : {
"valid_children" : ["leaf"]
},
"leaf" : {
"valid_children" : []
}
},
"plugins" : ["types", "dnd", "contextmenu"]});
Also, you can override "contextmenu"
"contextmenu":{
"items": function () {
return {
"Create": {
"label": "Create",
"action": function (data) {
var ref = $.jstree.reference(data.reference);
sel = ref.get_selected();
if(!sel.length) { return false; }
sel = sel[0];
type = ref.get_type(sel);
if (type == "#")
type = "branch";
else if (type == "branch")
type = "leaf";
else if (type == "leaf")
type = "";
sel = ref.create_node(sel, {text: "new "+type, type: type});
if(sel) {
ref.edit(sel);
}
}
},
"Rename": {
"label": "Rename",
"action": function (data) {
var inst = $.jstree.reference(data.reference);
obj = inst.get_node(data.reference);
inst.edit(obj);
}
},
"Delete": {
"label": "Delete",
"action": function (data) {
var ref = $.jstree.reference(data.reference),
sel = ref.get_selected();
if(!sel.length) { return false; }
ref.delete_node(sel);
}
}
};
}
},
You have a problem with "types". The "Create" action in the "contextmenu" does not know about the types of "branch" and "leaf" and create new node with "type" : "default".
You can see this:
"types" : {
"#" : {
"valid_children" : ["branch", "default"]
},
"branch" : {
"valid_children" : ["leaf", "default"]
},
"leaf" : {
"valid_children" : []
}
},

Filtering realtime database data

I have the following firebase database. I would like to filter and list the orders which's deliveryDuration value is "45"
"orders" : [ null, {
"comment" : "",
"date" : "2018-07-09 10:07:18",
"deliveryDuration" : "45",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Dessert",
"details" : "(Hausgemacht)",
"name" : "Warmer Topfenstrudel",
"price" : 3.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}, {
"comment" : "",
"date" : "2018-07-10 10:07:33",
"deliveryDuration" : "30",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Dessert",
"details" : "(Hausgemacht)",
"name" : "Warmer Topfenstrudel",
"price" : 3.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}, {
"comment" : "",
"date" : "2018-07-10 10:13:13",
"deliveryDuration" : "10",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Spezialitäten",
"details" : "Schweinschnitzel mit Parmesan gebacken auf Spaghetti Napoli",
"name" : "Piccata Milanese",
"price" : 12.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}
}
I tried writing the following function:
getNewItems: function (order) {
if (db.ref('orders').child(order).child('deliveryDuration').equalTo(45)){
return db.ref('orders').child(order).child('deliveryDuration')
}}
How could I get the filtered objects? I was thinking that that i write an if-else statement and the function returns the values what i was looking for.
I think you're looking for this:
getNewItems: function (order) {
return db.ref('orders').orderByChild('deliveryDuration').equalTo(45));
}}
This Firebase query takes each child node of the location you query, orders it by the deliveryDuration property, and then filters to only get the ones who are equal to 45.

Javascript menu recursive function bugs

//current jsfiddle
http://jsfiddle.net/0ht35rpb/45/
I am trying to loop through a json navigation tree - so that when a person goes to a particular page, it finds its alternative language counter-part.
//JSON
{
"langs" : [
{
"lang" : "de",
"lines" : {
"menu" : [
{
"title" : "Anleitung",
"link" : "/de/anleitung"
},
{
"title" : "Funktionen",
"link" : "/de/funktionen"
},
{
"title" : "Dienstleistungen",
"link" : "/de/dienstleistungen",
"children" : [
{
"title" : "Geistiges Eigentum",
"link" : "/de/dienstleistungen/geistiges-eigentum"
},
{
"title" : "Compliance",
"link" : "/de/dienstleistungen/compliance"
},
{
"title" : "Investment- und Beteiligungsrecht",
"link" : "/de/dienstleistungen/beteiligungsrecht"
},
{
"title" : "Mergers & Acquisitions",
"link" : "/de/dienstleistungen/mergers-and-acquisitions"
},
{
"title" : "Immobilienrecht",
"link" : "/de/dienstleistungen/immobilienrecht"
},
{
"title" : "Internet- und Datenschutzrecht",
"link" : "/de/dienstleistungen/internetrecht"
},
{
"title" : "Gesellschaftsrecht",
"link" : "/de/dienstleistungen/gesellschaftsrecht"
},
{
"title" : "Handelsrecht",
"link" : "/de/dienstleistungen/handelsrecht"
},
{
"title" : "Arbeitsrecht",
"link" : "/de/dienstleistungen/arbeitsrecht"
},
{
"title" : "Bankrecht",
"link" : "/de/dienstleistungen/bankrecht"
},
{
"title" : "Vertragsrecht",
"link" : "/de/dienstleistungen/vertragsrecht"
},
{
"title" : "Wettbewerbsrecht",
"link" : "/de/dienstleistungen/wettbewerbsrecht"
}
]
},
{
"title" : "Beliebte Projekte",
"link" : "/de/beliebte-projekte",
"children" : [
{
"title" : "Compliance",
"link" : "/de/beliebte-projekte/compliance",
"children" : [
{
"title" : "Haftungsrisiken für Geschäftsführern",
"link" : "/de/beliebte-projekte/compliance/haftungsrisken-geschaeftsfuehrern"
},
{
"title" : "Compliance-Prüfung KMU",
"link" : "/de/beliebte-projekte/compliance/compliance-pruefung-kmu"
}
]
}
]
}
],
"sign_in" : "Login"
}
},
{
"lang" : "en",
"lines" : {
"menu" : [
{
"title" : "How it works",
"link" : "/en/how-it-works"
},
{
"title" : "Features",
"link" : "/en/features"
},
{
"title" : "Services",
"link" : "/en/services",
"children" : [
{
"title" : "Intellectual property",
"link" : "/en/services/intellectual-property"
},
{
"title" : "Compliance",
"link" : "/en/services/compliance"
},
{
"title" : "Investment law",
"link" : "/en/services/investment-law"
},
{
"title" : "Mergers & Acquisitions",
"link" : "/en/services/mergers-and-acquisitions"
},
{
"title" : "Real estate law",
"link" : "/en/services/real-estate-law"
},
{
"title" : "Internet law and data privacy",
"link" : "/en/services/internet-law"
},
{
"title" : "Company law",
"link" : "/en/services/company-law"
},
{
"title" : "Trade law",
"link" : "/en/services/trade-law"
},
{
"title" : "Labour law",
"link" : "/en/services/labour-law"
},
{
"title" : "Bank law",
"link" : "/en/services/bank-law"
},
{
"title" : "Contract law",
"link" : "/en/services/contract-law"
},
{
"title" : "Competition law",
"link" : "/en/services/competition-law"
}
]
},
{
"title" : "Popular Projects",
"link" : "/en/popular-projects",
"children" : [
{
"title" : "Compliance",
"link" : "/en/popular-projects/compliance",
"children" : [
{
"title" : "Haf eng",
"link" : "/en/popular-projects/compliance/haf-eng"
},
{
"title" : "Compliance eng",
"link" : "/en/popular-projects/compliance/compliance-eng"
}
]
}
]
}
],
"sign_in" : "Sign in"
}
}
]
}
my js functions
so in this case
imagine
CURRENTLNG as en
CURRENTURL as /en/services
in fetchFooterUrls () - I want to return an array with ["/en/services", "/de/dienstleistungen"]
this code falls down in trying to get the 3rd level navigation counter parts
["/en/popular-projects/compliance/compliance-eng", "/de/beliebte-projekte/compliance/compliance-pruefung-kmu"]
getUrl (pairUrl, currentLng, enMenu, deMenu, obj) {
for (let k in obj) {
if (!obj.hasOwnProperty(k)) continue
if (obj[k].link === pairUrl) {
if (currentLng === 'de') {
return enMenu[k].link // get en link equivlant
} else {
return deMenu[k].link // get de link equivlant
}
} else {
if (obj[k].hasOwnProperty('children') && obj[k].children.length > 0) continue
this.getUrl(pairUrl, currentLng, enMenu[k].children, deMenu[k].children, obj[k].children)
}
}
}
//
getLanguagePair (currentLng, pairUrl) {
// 'find url in json tree'
var enMenu = linkTreeObject.langs[1].lines.menu
var deMenu = linkTreeObject.langs[0].lines.menu
let obj = {}
// find position in tree
if (currentLng === 'de') {
obj = deMenu
} else {
obj = enMenu
}
return this.getUrl(pairUrl, currentLng, enMenu, deMenu, obj)
}
fetchFooterUrls () {
let deUrl = ''
let enUrl = ''
if (CURRENTLNG === 'de') {
deUrl = CURRENTURL
enUrl = this.getLanguagePair(CURRENTLNG, this.props.location.pathname)
} else {
enUrl = CURRENTURL
deUrl = this.getLanguagePair(CURRENTLNG, this.props.location.pathname)
}
return [enUrl, deUrl]
}
//tried to make this snippet - any moderators here to resolve?
var linkTreeObject ={
"langs" : [
{
"lang" : "de",
"lines" : {
"menu" : [
{
"title" : "Anleitung",
"link" : "/de/anleitung"
},
{
"title" : "Funktionen",
"link" : "/de/funktionen"
},
{
"title" : "Dienstleistungen",
"link" : "/de/dienstleistungen",
"children" : [
{
"title" : "Geistiges Eigentum",
"link" : "/de/dienstleistungen/geistiges-eigentum"
},
{
"title" : "Compliance",
"link" : "/de/dienstleistungen/compliance"
},
{
"title" : "Investment- und Beteiligungsrecht",
"link" : "/de/dienstleistungen/beteiligungsrecht"
},
{
"title" : "Mergers & Acquisitions",
"link" : "/de/dienstleistungen/mergers-and-acquisitions"
},
{
"title" : "Immobilienrecht",
"link" : "/de/dienstleistungen/immobilienrecht"
},
{
"title" : "Internet- und Datenschutzrecht",
"link" : "/de/dienstleistungen/internetrecht"
},
{
"title" : "Gesellschaftsrecht",
"link" : "/de/dienstleistungen/gesellschaftsrecht"
},
{
"title" : "Handelsrecht",
"link" : "/de/dienstleistungen/handelsrecht"
},
{
"title" : "Arbeitsrecht",
"link" : "/de/dienstleistungen/arbeitsrecht"
},
{
"title" : "Bankrecht",
"link" : "/de/dienstleistungen/bankrecht"
},
{
"title" : "Vertragsrecht",
"link" : "/de/dienstleistungen/vertragsrecht"
},
{
"title" : "Wettbewerbsrecht",
"link" : "/de/dienstleistungen/wettbewerbsrecht"
}
]
},
{
"title" : "Beliebte Projekte",
"link" : "/de/beliebte-projekte",
"children" : [
{
"title" : "Compliance",
"link" : "/de/beliebte-projekte/compliance",
"children" : [
{
"title" : "Haftungsrisiken für Geschäftsführern",
"link" : "/de/beliebte-projekte/compliance/haftungsrisken-geschaeftsfuehrern"
},
{
"title" : "Compliance-Prüfung KMU",
"link" : "/de/beliebte-projekte/compliance/compliance-pruefung-kmu"
}
]
}
]
}
],
"sign_in" : "Login"
}
},
{
"lang" : "en",
"lines" : {
"menu" : [
{
"title" : "How it works",
"link" : "/en/how-it-works"
},
{
"title" : "Features",
"link" : "/en/features"
},
{
"title" : "Services",
"link" : "/en/services",
"children" : [
{
"title" : "Intellectual property",
"link" : "/en/services/intellectual-property"
},
{
"title" : "Compliance",
"link" : "/en/services/compliance"
},
{
"title" : "Investment law",
"link" : "/en/services/investment-law"
},
{
"title" : "Mergers & Acquisitions",
"link" : "/en/services/mergers-and-acquisitions"
},
{
"title" : "Real estate law",
"link" : "/en/services/real-estate-law"
},
{
"title" : "Internet law and data privacy",
"link" : "/en/services/internet-law"
},
{
"title" : "Company law",
"link" : "/en/services/company-law"
},
{
"title" : "Trade law",
"link" : "/en/services/trade-law"
},
{
"title" : "Labour law",
"link" : "/en/services/labour-law"
},
{
"title" : "Bank law",
"link" : "/en/services/bank-law"
},
{
"title" : "Contract law",
"link" : "/en/services/contract-law"
},
{
"title" : "Competition law",
"link" : "/en/services/competition-law"
}
]
},
{
"title" : "Popular Projects",
"link" : "/en/popular-projects",
"children" : [
{
"title" : "Compliance",
"link" : "/en/popular-projects/compliance",
"children" : [
{
"title" : "Haf eng",
"link" : "/en/popular-projects/compliance/haf-eng"
},
{
"title" : "Compliance eng",
"link" : "/en/popular-projects/compliance/compliance-eng"
}
]
}
]
}
],
"sign_in" : "Sign in"
}
}
]
};
getUrl (pairUrl, currentLng, enMenu, deMenu, obj) {
for (let k in obj) {
if (!obj.hasOwnProperty(k)) continue
if (obj[k].link === pairUrl) {
if (currentLng === 'de') {
return enMenu[k].link // get en link equivlant
} else {
return deMenu[k].link // get de link equivlant
}
} else {
if (obj[k].hasOwnProperty('children') && obj[k].children.length > 0) continue
this.getUrl(pairUrl, currentLng, enMenu[k].children, deMenu[k].children, obj[k].children)
}
}
}
//
getLanguagePair (currentLng, pairUrl) {
// 'find url in json tree'
var enMenu = linkTreeObject.langs[1].lines.menu
var deMenu = linkTreeObject.langs[0].lines.menu
let obj = {}
// find position in tree
if (currentLng === 'de') {
obj = deMenu
} else {
obj = enMenu
}
return this.getUrl(pairUrl, currentLng, enMenu, deMenu, obj)
}
console.log(getLanguagePair("en", "/en/how-it-works"))
Updated the jsfiddle. You had errors related to the recursive function call in the else part.
http://jsfiddle.net/gaganshera/0ht35rpb/51/
Changed it to
if (!obj[k].hasOwnProperty('children') || obj[k].children.length <= 0) continue;
var ret = getUrl(pairUrl, currentLng, enMenu[k].children, deMenu[k].children, obj[k].children);
if(typeof ret != 'undefined') return ret;
var linkTreeObject = {
"langs": [{
"lang": "de",
"lines": {
"menu": [{
"title": "Anleitung",
"link": "/de/anleitung"
}, {
"title": "Funktionen",
"link": "/de/funktionen"
}, {
"title": "Dienstleistungen",
"link": "/de/dienstleistungen",
"children": [{
"title": "Geistiges Eigentum",
"link": "/de/dienstleistungen/geistiges-eigentum"
}, {
"title": "Compliance",
"link": "/de/dienstleistungen/compliance"
}, {
"title": "Investment- und Beteiligungsrecht",
"link": "/de/dienstleistungen/beteiligungsrecht"
}, {
"title": "Mergers & Acquisitions",
"link": "/de/dienstleistungen/mergers-and-acquisitions"
}, {
"title": "Immobilienrecht",
"link": "/de/dienstleistungen/immobilienrecht"
}, {
"title": "Internet- und Datenschutzrecht",
"link": "/de/dienstleistungen/internetrecht"
}, {
"title": "Gesellschaftsrecht",
"link": "/de/dienstleistungen/gesellschaftsrecht"
}, {
"title": "Handelsrecht",
"link": "/de/dienstleistungen/handelsrecht"
}, {
"title": "Arbeitsrecht",
"link": "/de/dienstleistungen/arbeitsrecht"
}, {
"title": "Bankrecht",
"link": "/de/dienstleistungen/bankrecht"
}, {
"title": "Vertragsrecht",
"link": "/de/dienstleistungen/vertragsrecht"
}, {
"title": "Wettbewerbsrecht",
"link": "/de/dienstleistungen/wettbewerbsrecht"
}]
}, {
"title": "Beliebte Projekte",
"link": "/de/beliebte-projekte",
"children": [{
"title": "Compliance",
"link": "/de/beliebte-projekte/compliance",
"children": [{
"title": "Haftungsrisiken für Geschäftsführern",
"link": "/de/beliebte-projekte/compliance/haftungsrisken-geschaeftsfuehrern"
}, {
"title": "Compliance-Prüfung KMU",
"link": "/de/beliebte-projekte/compliance/compliance-pruefung-kmu"
}]
}]
}],
"sign_in": "Login"
}
}, {
"lang": "en",
"lines": {
"menu": [{
"title": "How it works",
"link": "/en/how-it-works"
}, {
"title": "Features",
"link": "/en/features"
}, {
"title": "Services",
"link": "/en/services",
"children": [{
"title": "Intellectual property",
"link": "/en/services/intellectual-property"
}, {
"title": "Compliance",
"link": "/en/services/compliance"
}, {
"title": "Investment law",
"link": "/en/services/investment-law"
}, {
"title": "Mergers & Acquisitions",
"link": "/en/services/mergers-and-acquisitions"
}, {
"title": "Real estate law",
"link": "/en/services/real-estate-law"
}, {
"title": "Internet law and data privacy",
"link": "/en/services/internet-law"
}, {
"title": "Company law",
"link": "/en/services/company-law"
}, {
"title": "Trade law",
"link": "/en/services/trade-law"
}, {
"title": "Labour law",
"link": "/en/services/labour-law"
}, {
"title": "Bank law",
"link": "/en/services/bank-law"
}, {
"title": "Contract law",
"link": "/en/services/contract-law"
}, {
"title": "Competition law",
"link": "/en/services/competition-law"
}]
}, {
"title": "Popular Projects",
"link": "/en/popular-projects",
"children": [{
"title": "Compliance",
"link": "/en/popular-projects/compliance",
"children": [{
"title": "Haf eng",
"link": "/en/popular-projects/compliance/haf-eng"
}, {
"title": "Compliance eng",
"link": "/en/popular-projects/compliance/compliance-eng"
}]
}]
}],
"sign_in": "Sign in"
}
}]
};
function getUrl(pairUrl, currentLng, enMenu, deMenu, obj) {
for (let k in obj) {
if (!obj.hasOwnProperty(k)) continue
if (obj[k].link === pairUrl) {
if (currentLng === 'de') {
return enMenu[k].link // get en link equivlant
} else {
return deMenu[k].link // get de link equivlant
}
} else {
if (!obj[k].hasOwnProperty('children') || obj[k].children.length <= 0) continue;
var ret = getUrl(pairUrl, currentLng, enMenu[k].children, deMenu[k].children, obj[k].children);
if(typeof ret != 'undefined') return ret;
}
}
}
function getLanguagePair(currentLng, pairUrl) {
// 'find url in json tree'
var enMenu = linkTreeObject.langs[1].lines.menu
var deMenu = linkTreeObject.langs[0].lines.menu
let obj = {}
// find position in tree
if (currentLng === 'de') {
obj = deMenu
} else {
obj = enMenu
}
return getUrl(pairUrl, currentLng, enMenu, deMenu, obj)
}
//works
console.log(getLanguagePair("en", "/en/how-it-works"))
console.log(getLanguagePair("en", "/en/popular-projects"))
console.log(getLanguagePair("de", "/de/anleitung"))
console.log(getLanguagePair("de", "/de/beliebte-projekte"))
//fail
console.log(getLanguagePair("en", "/en/services/compliance"))
console.log(getLanguagePair("en", "/en/popular-projects/compliance"))
console.log(getLanguagePair("en", "/en/popular-projects/compliance/compliance-eng"))
there is a problem in your recursive function. If you don't match the url at the first function call, you recursively call the function again but you don't return it.
function get_10_recursive(number){
if(number>=10) return 10;
else return get_10_recursive(number++);
}
In this example, if the number is smaller than 10, the function is recursively called until it encounter a return statement, after this, it retrace the stack and returns you the correct result. If you remove the return statement in the third line the code is executed as well but it returns the result only of the first call, which is undefined.
The problem in your code is that, if you place a return inside the for loop, the loop gets interrupted and you won't check the next strings, so you have to store the results of the function call in a temporary variable and return it only if it's truthy (aka it's not undefined).
Your getUrl function becomes like this:
function getUrl(pairUrl, currentLng, enMenu, deMenu, obj) {
for (var k in obj) {
if (obj[k].link === pairUrl) {
if (currentLng === 'de') {
return enMenu[k].link; // get en link equivlant
} else {
return deMenu[k].link; // get de link equivlant
}
} else {
if (obj[k].hasOwnProperty('children')){
var tmp = getUrl(pairUrl, currentLng, enMenu[k].children, deMenu[k].children, obj[k].children);
if(tmp) return tmp; // check if it found the match successfully
}
}
}
}
PS: I remvoed the if (!obj.hasOwnProperty(k)) continue because it was useless

RedQueryBuilder validations

I am using the RedQueryBuilder version 0.5.0
http://0-5-0.redquerybuilder.appspot.com/ for building an Expression validator.
I want to Know, how to validate the entered expressions?
The metadata of the RedQuery Builder 0.5.0 supports css and class additions for Jquery support.
Please, suggest how to add the css metadata to the existing metadata.
The Sample MetaData is given below:
meta : {
tables : [ {
"name" : "PERSON",
"label" : "Person",
"columns" : [ {
"name" : "NAME",
"label" : "Name",
"type" : "STRING",
"size" : 10
}, {
"name" : "DOB",
"label" : "Date of birth",
"type" : "DATE"
}, {
"name" : "SEX",
"label" : "Sex",
"type" : "STRING",
"editor" : "SELECT"
}, {
"name" : "CATEGORY",
"label" : "Category",
"type" : "REF",
} ],
fks : []
} ],
types : [ {
"name" : "STRING",
"editor" : "TEXT",
"operators" : [ {
"name" : "=",
"label" : "is",
"cardinality" : "ONE"
}, {
"name" : "<>",
"label" : "is not",
"cardinality" : "ONE"
}, {
"name" : "LIKE",
"label" : "like",
"cardinality" : "ONE"
}, {
"name" : "<",
"label" : "less than",
"cardinality" : "ONE"
}, {
"name" : ">",
"label" : "greater than",
"cardinality" : "ONE"
} ]
}, {
"name" : "DATE",
"editor" : "DATE",
"operators" : [ {
"name" : "=",
"label" : "is",
"cardinality" : "ONE"
}, {
"name" : "<>",
"label" : "is not",
"cardinality" : "ONE"
}, {
"name" : "<",
"label" : "before",
"cardinality" : "ONE"
}, {
"name" : ">",
"label" : "after",
"cardinality" : "ONE"
} ]
}, {
"name" : "REF",
"editor" : "SELECT",
"operators" : [ {
"name" : "IN",
"label" : "any of",
"cardinality" : "MULTI"
}]
} ]
},

Categories

Resources