I have a screen that needs to calculate prices based on two different parameters, primary (activity in example) and children (ticketType in example)
Previously this was done hackishly, so i decided the best option would be to store the prices in a JSON object populated at runtime, and based on clicks & change events
So i have this json format I formulated, please note you can change this as well, not set in stone
{
"activities": [{
"activitycode": "TK",
"desc": "Tickets",
"ticketTypes": [{
"ticketcode": "G",
"baseprice": 79
}, {
"ticketcode": "P",
"baseprice": 109
}]
}, {
"activitycode": "PK",
"desc": "Parking",
"ticketTypes": [{
"ticketcode": "BK",
"baseprice": 65
}, {
"ticketcode": "ESC-I",
"baseprice": 40
}]
}], //end activities
"handlingPercent": "0.03",
"shipping": "15"
}
So, I tried traverse the JSON object for the correct price, like this
pricing.activities.activitycode['TK'].ticketTypes.ticketcode['G'].baseprice
but this doesnt work, because I havent specified which numerical node to go to yet before supplying my values
Once i did that, the following works
pricing.activities[0].ticketTypes[0].baseprice
But what i would like to accomplish is supplying string/text values, traverse parent nodes, then child nodes, and find the value
I suppose i could do an $.each iteration like this, but i wanted to know what the experts thought first, if this is indeed the best way to go about things
$.each(pricing.activities, function(arrayID, activityData) {
if(activityData.activitycode=="TK")
$.each(activityData.ticketTypes, function(ticketTypeID, typeData) {
if(typeData.ticketcode=="G")
alert(typeData.baseprice);
});
});
Before talking about the code, let's talk about your JSON data.
First, I reformatted the data so I could follow the structure more easily:
{
"activities": [
{
"activitycode": "TK",
"desc": "Tickets",
"ticketTypes": [
{ "ticketcode": "G", "baseprice": 79 },
{ "ticketcode": "P", "baseprice": 109 }
]
},
{
"activitycode": "PK",
"desc": "Parking",
"ticketTypes": [
{ "ticketcode": "BK", "baseprice": 65 },
{ "ticketcode": "ESC-I", "baseprice": 40 }
]
}
],
"handlingPercent": "0.03",
"shipping": "15"
}
Now that the structure is clear, here is the question: You have an activities array, and inside each of the elements of that array there is a ticketTypes array.
Does the order of items in these arrays matter? i.e. are they used to generate a display list that must be in the correct order?
And, how do you need to access these arrays? Do you primarily loop through them and do something with all the elements? Or do you use them to look up individual elements given a known activitycode and ticketcode - which is what the nested $.each loops at the end of your question end up doing?
Depending on the answers to these questions, you may be better served with a different kind of structure. Perhaps like this:
{
"activities": {
"TK": {
"desc": "Tickets",
"ticketTypes": {
"G": { "baseprice": 79 },
"P": { "baseprice": 109 }
}
},
"PK": {
"desc": "Parking",
"ticketTypes": {
"BK": { "baseprice": 65 },
"ESC-I": { "baseprice": 40 }
}
}
},
"handlingPercent": "0.03",
"shipping": "15"
}
Because now this code (I cleaned up the indentation here too):
$.each( pricing.activities, function( arrayID, activityData ) {
if( activityData.activitycode=="TK" ) {
$.each( activityData.ticketTypes, function( ticketTypeID, typeData ) {
if( typeData.ticketcode=="G" )
alert( typeData.baseprice );
});
}
});
can be replaced with:
alert( pricing.activities.TK.ticketTypes.G.baseprice );
and other similar code that you write will also be simpler. About the only thing you lose this way is the guarantee of ordering of the activities and ticketTypes: arrays have a guaranteed order, objects do not.
Have you tried $.grep()?
var test = $.grep(pricing.activities, function(e){ return e.activitycode == 'TK'; });
var test2 = $.grep(test[0].ticketTypes, function(e){ return e.ticketcode == "G";});
console.log(test2[0].baseprice);
Fiddle Here. jQuery API Documentation on $.grep() here
Example from this question
I wouldn't say it's better than $.each(), but it's another method, if you're into that sort of thing.
Related
I want to create a JSON API that returns a list of objects. Each object has an id, a name and some other information. API is consumed using JavaScript.
The natural options for my JSON output seems to be:
"myList": [
{
"id": 1,
"name": "object1",
"details": {}
},
{
"id": 2,
"name": "object2",
"details": {}
},
{
"id": 3,
"name": "object3",
"details": {}
},
]
Now let's imagine that I use my API to get all the objects but want to first do something with id2 then something else with id1 and id3.
Then I may be interested to be able to directly get the object for a specific id:
"myList": {
"1": {
"name": "object1",
"details": {}
},
"2": {
"name": "object2",
"details": {}
},
"3": {
"name": "object3",
"details": {}
},
}
This second option may be less natural when somewhere else in the code I want to simply loop through all the elements.
Is there a good practice for these use cases when the API is used for both looping through all elements and sometime using specific elements only (without doing a dedicated call for each element)?
In your example you've changed the ID value from 1 to id1. This would make operating on the data a bit annoying, because you have to add and remove id all the time.
If you didn't do that, and you were relying on the sorted order of the object, you may be in for a surprise, depending on JS engine:
var source = JSON.stringify({z: "first", a: "second", 0: "third"});
var parsed = JSON.parse(source);
console.log(Object.keys(parsed));
// ["0", "z", "a"]
My experience is to work with arrays on the transport layer and index the data (i.e. convert array to map) when required.
I'm trying to join a two tables (versus and words) on multiple ids. I don't really know how to explain it, so I'm just going to show what I mean.
An excerpt from the versus-table:
{
"date": 1427675857789,
"hero": "7b88a237-c288-48f1-bf45-2dcd9f812b54",
"id": "017fe06a-e37d-4f23-92a3-bc52b38de4d7",
"nemesis": "e87a6252-6d08-4c5a-b057-2718e8c07d93",
"points": {
"hero": 58659,
"nemesis": 3021
}
}
Excerpt from the words-table:
{
"id": "7b88a237-c288-48f1-bf45-2dcd9f812b54" ,
"word": "i"
},
{
"id": "e87a6252-6d08-4c5a-b057-2718e8c07d93" ,
"word": "the"
}
I'd like to join the two tables so get something like this:
{
"date": 1427675857789,
"hero": "i",
"nemesis": "the",
"points": {
"hero": 58659,
"nemesis": 3021
}
}
This is what I have so far: r.table("versus").eqJoin("hero", r.table("words")).zip(), which gets me this:
{
"date": 1427675857789 ,
"hero": "7b88a237-c288-48f1-bf45-2dcd9f812b54" ,
"id": "7b88a237-c288-48f1-bf45-2dcd9f812b54" ,
"nemesis": "e87a6252-6d08-4c5a-b057-2718e8c07d93" ,
"points": {
"hero": 60507 ,
"nemesis": 3504
} ,
"word": "i"
}
I'm a little puzzled about how I can join it on the hero-row as well as nemesis-row.
Though I'd be happy with any result that shows most of the things from the versus-table (Doesn't matter if id is there) and two the two words which corresponds to the hero and nemesis id.
EDIT: I've figured something out, but now I'm only able to get the first document, which kind of defeats the purpose of what I'm trying to do... Here's what I got: r.table("versus").eqJoin("hero", r.table("words")).zip().map(r.row.merge({hero: r.row("word")})).eqJoin("nemesis", r.table("words")).zip().map(r.row.merge({nemesis: r.row("word")})).without(["word", "id"])
Well, I did it...finally!
If someone's interested, this is what my new ReQL looks like:
r.table("versus").concatMap(function(v){
return r.table("words").getAll(v("hero"), {index: "id"}).map(function(w){
return r.branch(
v("hero").eq(w("id")),
v.merge({hero: w("word")}),
v
)
})
}).concatMap(function(v){
return r.table("words").getAll(v("nemesis"), {index: "id"}).map(function(w){
return r.branch(
v("nemesis").eq(w("id")),
v.merge({nemesis: w("word")}),
v
)
})
}).without("id")
Consider this example collection:
{
"_id:"0,
"firstname":"Tom",
"children" : {
"childA":{
"toys":{
'toy 1':'batman',
'toy 2':'car',
'toy 3':'train',
}
"movies": {
'movie 1': "Ironman"
'movie 2': "Deathwish"
}
},
"childB":{
"toys":{
'toy 1':'doll',
'toy 2':'bike',
'toy 3':'xbox',
}
"movies": {
'movie 1': "Frozen"
'movie 2': "Barbie"
}
}
}
}
Now I would like to retrieve ONLY the movies from a particular document.
I have tried something like this:
movies = users.find_one({'_id': 0}, {'_id': 0, 'children.ChildA.movies': 1})
However, I get the whole field structure from 'children' down to 'movies' and it's content. How do I just do a query and retrieve only the content of 'movies'?
To be specific I want to end up with this:
{
'movie 1': "Frozen"
'movie 2': "Barbie"
}
The problem here is your current data structure is not really great for querying. This is mostly because you are using "keys" to actually represent "data points", and while it might initially seem to be a logical idea it is actually a very bad practice.
So rather than do something like assign "childA" and "childB" as keys of an object or "sub-document", you are better off assigning these are "values" to a generic key name in a structure like this:
{
"_id:"0,
"firstname":"Tom",
"children" : [
{
"name": "childA",
"toys": [
"batman",
"car",
"train"
],
"movies": [
"Ironman"
"Deathwish"
]
},
{
"name": "childB",
"toys": [
"doll",
"bike",
"xbox",
],
"movies": [
"Frozen",
"Barbie"
]
}
]
}
Not the best as there are nested arrays, which can be a potential problem but there are workarounds to this as well ( but later ), but the main point here is this is a lot better than defining the data in "keys". And the main problem with "keys" that are not consistently named is that MongoDB does not generally allow any way to "wildcard" these names, so you are stuck with naming and "absolute path" in order to access elements as in:
children -> childA -> toys
children -> childB -> toys
And that in a nutshell is bad, and compared to this:
"children.toys"
From the sample prepared above, then I would say that is a whole lot better approach to organizing your data.
Even so, just getting back something such as a "unique list of movies" is out of scope for standard .find() type queries in MongoDB. This actually requires something more of "document manipulation" and is well supported in the aggregation framework for MongoDB. This has extensive capabilities for manipulation that is not present in the query methods, and as a per document response with the above structure then you can do this:
db.collection.aggregate([
# De-normalize the array content first
{ "$unwind": "$children" },
# De-normalize the content from the inner array as well
{ "$unwind": "$children.movies" },
# Group back, well optionally, but just the "movies" per document
{ "$group": {
"_id": "$_id",
"movies": { "$addToSet": "$children.movies" }
}}
])
So now the "list" response in the document only contains the "unique" movies, which corresponds more to what you are asking. Alternately you could just $push instead and make a "non-unique" list. But stupidly that is actually the same as this:
db.collection.find({},{ "_id": False, "children.movies": True })
As a "collection wide" concept, then you could simplify this a lot by simply using the .distinct() method. Which basically forms a list of "distinct" keys based on the input you provide. This playes with arrays really well:
db.collection.distinct("children.toys")
And that is essentially a collection wide analysis of all the "distinct" occurrences for each"toys" value in the collection, and returned as a simple "array".
But as for you existing structure, it deserves a solution to explain, but you really must understand that the explanation is horrible. The problem here is that the "native" and optimized methods available to general queries and aggregation methods are not available at all and the only option available is JavaScript based processing. Which even though a little better through "v8" engine integration, is still really a complete slouch when compared side by side with native code methods.
So from the "original" form that you have, ( JavaScript form, functions have to be so easy to translate") :
db.collection.mapReduce(
// Mapper
function() {
var id this._id;
children = this.children;
Object.keys(children).forEach(function(child) {
Object.keys(child).forEach(function(childKey) {
Object.keys(childKey).forEach(function(toy) {
emit(
id, { "toys": [children[childkey]["toys"][toy]] }
);
});
});
});
},
// Reducer
function(key,values) {
var output = { "toys": [] };
values.forEach(function(value) {
value.toys.forEach(function(toy) {
if ( ouput.toys.indexOf( toy ) == -1 )
output.toys.push( toy );
});
});
},
{
"out": { "inline": 1 }
}
)
So JavaScript evaluation is the "horrible" approach as this is much slower in execution, and you see the "traversing" code that needs to be implemented. Bad news for performance, so don't do it. Change the structure instead.
As a final part, you could model this differently to avoid the "nested array" concept. And understand that the only real problem with a "nested array" is that "updating" a nested element is really impossible without reading in the whole document and modifying it.
So $push and $pull methods work fine. But using a "positional" $ operator just does not work as the "outer" array index is always the "first" matched element. So if this really was a problem for you then you could do something like this, for example:
{
"_id:"0,
"firstname":"Tom",
"childtoys" : [
{
"name": "childA",
"toy": "batman"
}.
{
"name": "childA",
"toy": "car"
},
{
"name": "childA",
"toy": "train"
},
{
"name": "childB",
"toy": "doll"
},
{
"name": "childB",
"toy": "bike"
},
{
"name": "childB",
"toy": "xbox"
}
],
"childMovies": [
{
"name": "childA"
"movie": "Ironman"
},
{
"name": "childA",
"movie": "Deathwish"
},
{
"name": "childB",
"movie": "Frozen"
},
{
"name": "childB",
"movie": "Barbie"
}
]
}
That would be one way to avoid the problem with nested updates if you did indeed need to "update" items on a regular basis rather than just $push and $pull items to the "toys" and "movies" arrays.
But the overall message here is to design your data around the access patterns you actually use. MongoDB does generally not like things with a "strict path" in the terms of being able to query or otherwise flexibly issue updates.
Projections in MongoDB make use of '1' and '0' , not 'True'/'False'.
Moreover ensure that the fields are specified in the right cases(uppercase/lowercase)
The query should be as below:
db.users.findOne({'_id': 0}, {'_id': 0, 'children.childA.movies': 1})
Which will result in :
{
"children" : {
"childA" : {
"movies" : {
"movie 1" : "Ironman",
"movie 2" : "Deathwish"
}
}
}
}
Consider a JSON like this:
[{
"type": "person",
"name": "Mike",
"age": "29"
},
{
"type": "person",
"name": "Afshin",
"age": "21"
},
{
"type": "something_else",
"where": "NY"
}]
I want to search in the JSON value with a key (for example type='person') and then select a whole object of matched item in JSON. For example when I search for type='person' I expect this value:
[{
"type": "person",
"name": "Mike",
"age": "29"
},
{
"type": "person",
"name": "Afshin",
"age": "21"
}]
Because it's a really big JSON value, I don't want to do a brute-force search in all nodes, so I think the only way is using Regular Expressions but I don't know how can I write a Regex to match something like above.
I'm using NodeJs for the application.
Using underscore.js#where:
var results = _(yourObject).where({ type: 'person' })
If your data set is very very big [e.g. 10k or so], consider filtering / paginating stuff server side.
Plain javascript :
var results = dataset.filter(function(p) {
if(p.type == 'person')
return true;
});
If the requirement is to scan multiple times through the collection, the following one time construction overhead might be of worth.
Use hashing based on values of type.Convert the current data structure to hash map.
var hashMap ={
};
hashMap['person'] =[{},{}];
Hope this helps you.
Use
$.grep(jsonarrayobj,function(n, i){
if(n.type==="person")
{}
})
I'm having trouble finding a solution that will help me loop through a bunch of elements and putting the chosen values into a table. I've been able to withdraw some values but the method isn't dynamic.
Here is an example:
var Table = {
"credit": {
"link": "site link",
"logoUrl": "logo url",
"message": "message"
},
"groups": [
{
"labels": [
{
"name": "Western Conference",
"type": "conference"
},
{
"name": "Central Division",
"type": "division"
}
],
"standings": [
{
"stats": [
{
"name": "gp",
"value": 20
},
{
"name": "w",
"value": 17
},
{
"name": "l",
"value": 0
},
{
"name": "gf",
"value": 64
},
{
"name": "ga",
"value": 37
},
{
"name": "gd",
"value": 27
},
{
"name": "pts",
"value": 37
}
],
"team": {
"id": 12345,
"link": "team link",
"name": "team name",
"shortName": "team"
}
},
This is the structure of the elements. So far I've used this:
document.getElementById("sGamesPlayed").innerHTML=Table.groups[0].standings[0].stats[0].value;
to withdraw values. However there are more teams, stats and divisions so I would need some kind of loop to go through the elements and put the into a dynamic table.
I would consider you to look at http://underscorejs.org/.
it provides a bunch of utility functions that could help you,
for example, _.each() helps you loop through JSON properties.
for the sample objects you've given (after completing the missing brackets at the end),
_.each(Table.groups[0].standings[0].stats, function(stats){
console.log(stats['name']+","+stats['value'])
})
gives me:
gp,20
w,17
l,0
gf,64
ga,37
gd,27
pts,37
how it works is that you provide the object you want as the first argument and the function that you give as the second argument will be called with each element of the first argument (Assuming it is a list).
I would also urge you to look at underscore templating that you can use to render your table where i put the console.log :
http://net.tutsplus.com/tutorials/javascript-ajax/getting-cozy-with-underscore-js/
http://scriptble.com/2011/01/28/underscore-js-templates/
I guess your question is about filtering the values of the array standings. In order to do that you can use the jQuery grep function (if you want to use jQuery).
For example you can write:
var arr = $.grep(Table.groups[0].standings[0].stats, function(d){return d.value>25})
Which will give
arr = [{"name": "gf","value": 64}, {"name": "ga", "value": 37},{"name": "gd", "value": 27},{"name": "pts", "value": 37}]
If this is not what you meant, can you please create a jsFiddle with a sample of what you want?
Depending on what you want to do with the results, you can go over the object using a scheme like:
var groups, standings, stats, value;
groups = Table.groups;
// Do stuff with groups
for (var i=0, iLen=groups.length; i<iLen; i++) {
standings = groups[i].standings;
// Do stuff with standings
for (var j=0, jLen=standings.length; j<jLen; j++) {
stats = standings[j];
// Do stuff with stats
for (var k=0, kLen=stats.length; k<kLen; k++) {
value = stats[k].value;
// Do stuff with value
}
}
}
Of course I have no idea what the data is for, what the overall structure is or how you want to present it. But if you have deeply nested data, all you can do is dig into it. You might be able to write a recursive function, but it might also become very difficult to maintain if the data structure is complex.