I'm looking for a way to store objects in a collection and identify them by UUID, then query the collection to get an object by UUID. The closest example I can think of is the Dictionary collection in .NET. Is there a recommended functionality in Dojo for this?
There is no equivalent in dojo, and even when the dojo/store/Memory module is not meant for this purpose, you can use it in some way as a collection, check this out:
require(["dojo/store/Memory"], function(Memory){
var someData = [
{id:1, name:"One"},
{id:2, name:"Two"}
];
store = new Memory({
idProperty: "id", //define the id property
data: someData
});
// Returns the object with an id of 1
store.get(1);
// Returns query results from the array that match the given query
store.query({name:"One"});
// Pass a function to do more complex querying
store.query(function(object){
return object.id > 1;
});
// Returns query results and sort by id
store.query({name:"One"}, {sort: [{attribute: "id"}]});
// store the object with the given identity
store.put({id:3, name:"Three"});
// delete the object
store.remove(3);
});
dojo has other types of store that may suit more to your case than the dojo/store/Memory. Here are some links to docs:
dojo/store/Memory
dojo/store/JsonRest
dojo/store/DataStore
dojo/store/Cache
Exist others, but this are the must commons
There is not such objects has Collection in JavaScript. But you could create your own Objects like below
var coll = { uid : "value" };
coll['uid2'] = "value2;
This can be accessed using
var a = coll['uid'];
This is the beauty of JavaScript.
Related
I have an array of array objects in the following form:
[[eventID, IP, timestamp], [eventID, IP, timestamp]... ]
Every so often I receive a new array object in the same format ([eventID, IP, timestamp]).
I need to check the main array for an existing object with the same eventID/IP pair (which is quite likely) without matching the timestamp.
Obviously I could iterate through the list checking first for eventID, then for IP but this seems... inelegant.
I could use indexOf but I'm not entirely sure of the syntax for a wildcard element in an array used to match elements in an array.
What would be the most efficient way to search the main array for an [eventID, IP, WILDCARD]?
You could use a two-level key/value structure for fast look-up. Traditionally one would do this with plain objects, but since ES6 it is advised to use Map objects for such dynamically growing structures.
You could for instance use an object that acts as a hash with 2 dimensions (eventId and IP), which exposes get and set methods, internally using nested Map objects:
function EventHash() {
var hash = new Map;
this.set = function (eventId, IP, timestamp) {
if (!hash.has(eventId)) hash.set(eventId, new Map);
hash.get(eventId).set(IP, timestamp);
return this;
};
this.get = function (eventId, IP) {
if (hash.has(eventId)) return hash.get(eventId).get(IP);
}
}
// Demo
var hash = new EventHash();
hash.set(1, '123.123.123.123', 11111);
hash.set(1, '555.555.555.555', 22222);
hash.set(2, '555.555.555.555', 33333);
console.log(hash.get(1, '123.123.123.123')); // 1111
hash.set(1, '123.123.123.123', 44444);
console.log(hash.get(1, '123.123.123.123')); // 4444
console.log(hash.get(5, '123.123.123.123')); // does not exist
console.log(hash.get(1, '')); // does not exist
I'm trying to use something as follows
collection.find({"myField."+variable : {$exists: true})
But obviously that doesn't work because the collections doesn't take strings. Instead, I tried building a a fully query string in JSON but that won't parse correctly because I'm just searching for a field name and not an entire object
var qry = '{"myField.'+variable+'"}'; //no go
I've also tried the meteor collection fields logic
var qry = 'myField.'+variable;
collection.find({}, {fields: {qry: 1}})
to no avail. I know that a query can take a JSON object but I'm not sure how to write this up.
Give this a try:
var selector = {};
selector["myField." + variable] = {$exists: true};
Collection.find(selector);
This can be accomplished using bracket notation
var fieldQuery = {};
fieldQuery["myField"+variable] = {$exists: true};
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors#Bracket_notation
I want to get references in mongoDB using nodejs/mongoose.
In the documentation I read that there are two options: Manual References or DBRefs.
Since they state, its recommended to use Manual References, I decided to set up a schema in the following way:
var schema = new mongoose.Schema({
name : String,
reference : mongoose.Schema.ObjectId
});
Question: If I retrieve an array of these objects from my collection, how do I resolve the references in a good practice way?
My Idea was to use Nimble and parallelize the necessary requests. I wanted to do something like
flow.parallel(functions, function() {
return result;
});
where I dynamically fill an array of functions
var functions = []
which I pass then to nimble.
(kind of this SO-question: Javascript Array of Functions)
Question: Is this practical? The array of functions-thing seems kind of not really the way to go to me. But I don't see any alternative, since nimble needs to be called with a static number of functions.
You can use Mongoose's support for reference population to efficiently follow references.
var schema = new mongoose.Schema({
name : String,
reference : { type: mongoose.Schema.ObjectId, ref: 'OtherModel' }
});
var MyModel = mongoose.model('MyModel', schema);
MyModel.find().populate('reference').exec(function(err, docs) {...});
In the above example, the reference field of each docs element gets populated with referenced doc.
I have two backbone collections Categories and Items, Categories contains category models with id, name, and items (w/c contains item ids in string format separated by commas) and Items collection that contains item models(id, name, etc.).
How do I merge them into one object that can be easily be rendered in a handlebars template
Sample Structure:
var Categories = [{'id':'1', 'category_name':'Burgers', 'category_items':'1,2'},{'id':'2','category_name':'Drinks','category_items':'3'}];
var Items = [{'id':'1','item_name':'Burger 1'},{'id':'1','item_name':'Burger 2'},{'id':'1','item_name':'Chicken; 1'}];
Sample Output:
var output = [{'id':'1', 'category_name':'Burgers', 'items':[{'id':'1','item_name':'Burger1', ...},{'id':'1','item_name':'Burger2', ...} ]}, {'id':'2', 'category_name':'Chicken', 'items':[{'id':'3','item_name':'Chicken1', ...} ]
And yes, i tried enough, but I can't seem to manage it
Relying on Underscore's helpful functions:
var json = _.map(c.toJSON(), function(c) {
return _.extend(c, {
category_items: _.map(c.category_items.split(','), function(id) {
return i.get(id).toJSON();
})
});
});
http://jsfiddle.net/626x9/1/
The first _.map coupled with _.extend just serves the purpose on replacing the category_items. I find it quite abusive, but can't remember if there's any method that would do that in one shot.
I write a function called "mergeObjectWithId". It merge data from two collection follow these step:
Using collection.findWhere() to get the target model which need to merge.
Using collection.where() to get the models in item collection.
Then assign the items array to target object's property items. Now you get the output object which you wanted.
Try this function:
function mergeObjectWithId(id){
//assume categories is your Category collection instance
var obj = categories.findWhere({id:id}).clone();
//assume items is your Item collection instance
obj.items = items.find({id:id}).slice(0);
return obj;
}
var output = mergeObjectWithId(1);
Hope this is helpful for you.
My goal is to take in a CSV file which contains approximately 4 million records and process each record while scrubbing the data of a particular field. The scrubbing process we have actually creates a reversible hash but is a time consuming process (almost 1 second). What I would like to do since there are only about 50,000 unique values for that field is to set them as properties of an object. Here is a pseudo example of how the object will be built. You can see that for duplicates I plan to just overwrite the existing value (this is to avoid having to loop through some if based search statement.
var csv = require('csv');
var http = require('http');
var CBNObj = new Object;
csv()
.fromPath(__dirname+'/report.csv',{
columns: true
})
.transform(function(data){
CBNObj[data['Field Value']] = data['Field Value'];
});
console.log(CBNObj);
This should create my object something like this.
myObj['fieldValue1'] = 'fieldValue1'
myObj['fieldValue2'] = 'fieldValue2'
myObj['fieldValue3'] = 'fieldValue3'
myObj['fieldValue1'] = 'fieldValue1'
myObj['fieldValue1'] = 'fieldValue1'
I have looked over some good posts on here about iterating over every property in an object (like this one Iterating over every property of an object in javascript using Prototype?) but I am still not exactly sure how to acccomplish what I am doing. How can I then take my object with 50k properties and essentially dump the values into an array so that I can end up with something like this?
myArray = ['fieldVaue1','fieldVaue2','fieldVaue3']
EDIT: I could also use some assistance on the first part here because I am getting a null value or undefined when I try and set the object properties. I also still need help then traversing through the object properties to build my array. Any help would be greatly appreciated.
You know that the keys of your object are the unique values you want. You just need an array. In node.js you can use Object.keys().
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
It's a standard way to take all the keys of an object (that aren't provided by the prototype chain) and put them into an array. So your example looks like this.
var csv = require('csv');
var AcctObj = new Object();
var uniqueArray;
csv()
.fromPath(__dirname+'/report.csv',{
columns: true
})
.on('data',function(data){
AcctObj[data['Some Field Value']] = data['Some Field Value'];
})
.on('end', function(){
uniqueArray = Object.keys(AcctObj);
});
Object.keys also does the hasOwnProperty check internally, so it's similar to the answer by #DvideBy0. It's just one step to the array you want.
var csv = require('csv');
var AcctObj = new Object();
csv()
.fromPath(__dirname+'/report.csv',{
columns: true
})
.on('data',function(data){
AcctObj[data['Some Field Value']] = data['Some Field Value'];
})
.on('end', function(){
for(var prop in AcctObj) {
if(AcctObj.hasOwnProperty(prop))
//Do something here....
}
});