I am using meteor for development which using mongo db as store. Thats means that all operations are just display object value (frontend) or operate object (backend). For example, from [user] collection "copy" value (userId) to [message] collection but different key name. is there any way better to
describe the relationship between two object rather than using
message.userId = user._id
maybe using a object to describe
{"userId","_id"}
There are many ways to skin a cat. This is how I would do it.
I would first create a mapping object with key-value pairs which represent the field relations between the two objects. The keys are the keys from the first object and the values are the keys from the second object.
{
"userId":"_id",
"userName":"name"
//...
}
Then I would use a function like this to apply the mapping object to two objects:
function applyMapping(fromObj, toObj, mappingObj) {
for (fromKey in mappingObj) {
var toKey = mappingObj[fromKey];
toObj[toKey] = fromObj[fromKey];
}
}
Related
I am new to JavaScript and Python, and programming in general.
I want to store data about common English phonograms in a JavaScript data object. There are about 80 phonograms. Each phonogram has one or more possible pronunciations. Each phonogram’s pronunciation would have a list of one or more word examples (say, 30 maximum) which would include the IPA phonetic symbols and a translation to a foreign language. E.g., the phonogram ‘ea’ has three pronunciation,
(1) 'iːˈ, (2)ˈɛˈ & (3)ˈeɪˈ:
(1)ˈbeadˈ, 'feat', 'beat'...
(2)'bread', 'head', 'dead'...
(3)'break'...
Is there a built-in data structure best suited for this? I am thinking of a class to make these objects and store it in an array or something. And how should I write my text for populating the the data objects?
JavaScript has four fundamental structured data types:
objects, which have properties, which have keys (names which are strings or Symbols) and values (any type)
arrays, which have elements, which have indexes and values (arrays are technically objects, but ignore that for now)
Map, which has entries, which have keys (any type) and values (any type)
Set, which has unique entries of any type (probably not useful for what you're doing)
It sounds like you'd probably want either an object or a Map where the keys are the phonographs and the values are objects. Within each object, you'd probably have another Map or object keyed by the pronunciation where the values are objects giving further information (examples and translations).
Here's an example using Maps, which you initialize by passing an array of arrays into the Map constructor:
const data = new Map([
[
"ea",
{
pronunciations: new Map([
[
"iː",
{
examples: ["bead", "feat"],
transations: [/*...*/]
}
]
]),
otherInfo: /*...*/
}
],
// ...the other 79 entries...
]);
Getting the data for an entry based on the phonogram:
const entry = data.get("ea");
The entry object will have a pronunciations property with a Map of the pronunciations and the objects (with examples and translations) they map to.
More on MDN:
Map
Object
Array
map of an array should do the work.
map key can contain an identifier while the map value is an array that can contain the pronunciation.
map is a (key, value) pair where value will be an array/[] in your case.
I have two variants to work with my objects full of data:
let global_obj = {
"id0": { /*big object 0*/},
"id1": { /*big object 1*/},
"id2": { /*big object 2*/}
};
and:
let global_arr = [
{ id: "id0" /*big object 0*/},
{ id: "id1" /*big object 1*/},
{ id: "id2" /*big object 2*/}
];
So I can save my big objects full of data inside of a global array or a global object. Which way is better in terms of performace (looping through, deleting, adding new objects)?
I am going to address specific objects by their id very often, change objects' properties, loop trought them all.
I would use an object in this case as finding an element by id is easier/faster. If index/order of elements is important, object don't help here and you need to use an array.
Basically:
Objects: Keyed collections
Arrays: Ordered collections
But regular objects and arrays are not the only options if the environment supports ES2015. ES2015 has introduces several APIs for working with collections: Map, Set, WeakMap and WeakSet.
As you mentioned that you'll be "addressing specific objects through their id's very often"; using an Object will be a wise choice for you. Since accessing an object's key is done in O(1) as compared to finding the object in an array, which will be O(n), hence Object will be better in performance.
You should do what's more convenient for you, it most likely won't make a difference.
However, generally, if the top level object is small, the array is always faster than a hashtable/object (the size of the inner objects doesn't matter). If the top-level structure is big, then you should consider the big-o complexity for the operations you want to perform on the top-level structure. See http://bigocheatsheet.com/
I am reading the book JavaScript: The Good Parts. It is said that
Objects in JavaScript are mutable keyed collections.
What does mutable keyed collection mean?
AS far as I could find on internet, mutable means the vales can be changes. I couldn't find what keyed collection mean.
Objects are a collection of keys with associated values. This could be referred to as a "keyed collection":
var o = {
foo: "bar",
bar: "baz"
}
(Where foo and bar here are keys).
...which can be changed (as you've already said, the "mutable" part):
o.foo = "foobar";
o.foobar = "bar";
The keyed keyword here means that the data is "named", "indexed" or "keyed".
{
key : value,
key2: value2
}
a collection because it contains a collection of data.
It is about the way objects work in Javascript, they behave like C# Dictionaries, for example, or named arrays in PHP. obj.someKey is equivalent to obj['someKey'] and you can always change the values associated with those keys or indeed delete them.
More advanced: the key uniquely identifies the value stored with it and the system is optimized for performance, so you can use this to index information or to get distinct values of a list,etc.
I have a Game class and a Roundclass. Game has a column named rounds which is an array of Round objects.
As there is a small and limited amount of them I chose Array over Parse.Relation, which I consider easy to use.
I have a Round object and I want to access the Gamewhich is his parent object.
How do I achieve that ?
If you're using the javascript API, I would use the Parse.Query.containsAll method.
This method takes two parameters, the key (field name) that must contain the object(s), and an array of values (in this case, the array will only contain one value.
var gameQuery = new Parse.Query("Game");
gameQuery.containsAll("rounds", [ round ]);
gameQuery.first().then
(
function( game )
{
//do stuff
}
);
I've never actually used this method for an array of pointers, though. you may need to pass an array containing just the object id of the round, rather than the pointer to the round. I'm not sure.
I have a custom object which contains other items (ie arrays, strings, other types of objects).
I am not sure how to traverse the object to iterate and list all of the object types, keys, and values of the nested items.
Second to this issue I don't know how many levels of nesting there are (as the object is generated dynamically from the back-end and passed to me as one object).
Any ideas (and should I just use javascript/jQuery or both to do this most efficiently)?
Thanks I'll give the code a go. I am retrieving a result set from a webservice which returns a different set of columns (of differing datatypes) and rows each time. I don't know the names of the columns which is why I am trying to get the data however I can.
Depending on the datatype I will perform a different action (sum the amount, format it etc).
JSON-serialized objects contain a hierarchy, w/o any reference cycles, so it should be fairly straightforward to traverse, something like
function visit(JSONobj, f)
{
for (var key in JSONobj)
{
var value = JSONobj[key];
f(key,value);
if (value instanceof Object)
visit(value, f);
}
}
where f is a function that does something with keys and values. (of course you could just write a function to do this directly).
What exactly are you trying to find within the object?