Posts

Showing posts with the label Object

Creating Classes And Objects Using Bash Scripting

Image
Answer : You can try to do something like this example.sh #!/bin/bash # include class header . obj.h . system.h # create class object obj myobject # use object method myobject.sayHello # use object property myobject.fileName = "file1" system.stdout.printString "value is" system.stdout.printValue myobject.fileName obj.h obj(){ . <(sed "s/obj/$1/g" obj.class) } obj.class # Class named "obj" for bash Object # property obj_properties=() # properties IDs fileName=0 fileSize=1 obj.sayHello(){ echo Hello } obj.property(){ if [ "$2" == "=" ] then obj_properties[$1]=$3 else echo ${obj_properties[$1]} fi } obj.fileName(){ if [ "$1" == "=" ] then obj.property fileName = $2 else obj.property fileName fi } system.h . system.class system.class system.stdout.printValue(){ echo $($@) } system.stdout.printString(){ echo $@ } Link for refer...

Convert Mongoose Docs To Json

Answer : You may also try mongoosejs's lean() : UserModel.find().lean().exec(function (err, users) { return res.end(JSON.stringify(users)); } Late answer but you can also try this when defining your schema. /** * toJSON implementation */ schema.options.toJSON = { transform: function(doc, ret, options) { ret.id = ret._id; delete ret._id; delete ret.__v; return ret; } }; Note that ret is the JSON'ed object, and it's not an instance of the mongoose model. You'll operate on it right on object hashes, without getters/setters. And then: Model .findById(modelId) .exec(function (dbErr, modelDoc){ if(dbErr) return handleErr(dbErr); return res.send(modelDoc.toJSON(), 200); }); Edit: Feb 2015 Because I didn't provide a solution to the missing toJSON (or toObject) method(s) I will explain the difference between my usage example and OP's usage example. OP: UserModel .find({}) // will get all...

Convert Map To JSON Object In Javascript

Answer : Given in MDN, fromEntries() is available since Node v12: const map1 = new Map([ ['foo', 'bar'], ['baz', 42] ]); const obj = Object.fromEntries(map1); // { foo: 'bar', baz: 42 } For converting object back to map: const map2 = new Map(Object.entries(obj)); // Map(2) { 'foo' => 'bar', 'baz' => 42 } I hope this function is self-explanatory enough. This is what I used to do the job. /* * Turn the map<String, Object> to an Object so it can be converted to JSON */ function mapToObj(inputMap) { let obj = {}; inputMap.forEach(function(value, key){ obj[key] = value }); return obj; } JSON.stringify(returnedObject) You could loop over the map and over the keys and assign the value function createPaths(aliases, propName, path) { aliases.set(propName, path); } var map = new Map(), object = {}; createPaths(map, 'paths.aliases.server.entry', 'src/test'); createPaths(ma...

Can I Loop Through A Javascript Object In Reverse Order?

Answer : Javascript objects don't have a guaranteed inherent order, so there doesn't exist a "reverse" order. 4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method. Browsers do seem to return the properties in the same order they were added to the object, but since this is not standard, you probably shouldn't rely on this behavior. A simple function that calls a function for each property in reverse order as that given by the browser's for..in, is this: // f is a function that has the obj as 'this' and the property name as first parameter function reverseForIn(obj, f) { var arr = []; for (var key in obj) { // add hasOwnPropertyCheck if needed arr.push(key); } for (var i=arr.length-1; i>=0; i--) { f.call(obj, arr[i]); } } //usage reverseFo...