NOT using reference to object (javascript) - javascript

The below :-
req.session.user = doc[0];
req.session.user.password = null;
Makes doc[0].password = null!
What do i do to prevent that?
Edit: Where doc[0] = { name: 'sangram', e-mail: 'abc.in', password : 'pwd' }

The only way to do so is to copy every property of an object:
req.session.user = {};
for (var key in user) {
if (key !== 'password') req.session.user[key] = user[key];
}
If user is a mongoose document, you probably should convert it to plain object first:
req.session.user = {};
var json = user.toObject();
for (var key in json) {
if (key !== 'password') req.session.user[key] = json[key];
}
But the easiest way to do what you want is to adopt some helpful library like underscore or lodash:
req.session.user = _.omit(user.toObject(), 'password');

For simple case of simple objects, I wrote a small helper function:
function ShallowClone(obj) {
var clone = {};
for (var key in obj)
clone[key] = obj[key];
return clone;
}
Usage in your case:
req.session.user = ShallowClone(doc[0]);
req.session.user.password = null; //won't affect doc[0] password
Live test case. (with dummy object)

I would recommend updating or changing your question in the future.
What is the most efficient way to deep clone an object in JavaScript?
How do I correctly clone a JavaScript object?
Crockford provides a very good answer here

You can't prevent this. The user property is set to that object doc[0]. Later you edit .password property of that newly assigned object. If you want to keep the password separately you have to assign doc[0] to another object.
req.session.user = {};
req.session.user.doc = doc[0];
req.session.user.password = null;
Here is a method for cloning/extending an object:
var extend = function() {
var process = function(destination, source) {
for (var key in source) {
if (hasOwnProperty.call(source, key)) {
destination[key] = source[key];
}
}
return destination;
};
var result = arguments[0];
for(var i=1; i<arguments.length; i++) {
result = process(result, arguments[i]);
}
return result;
};
And its usage:
var original = { value: "bla" };
var duplication = extend({}, original, { newProp: "test"});

Related

Recursively check for property on prototype and delete it from prototype if it exists

I am trying to write code to delete a property in an object. If it is inherited, it must go up the chain and delete itfrom the ancestor.
So far I have come up with this (doesnot work):
// My objects:
var pro = {'a':1};
var pro2 = Object.create(pro);
var pro3 = Object.create(pro2);
// -----------------------------------------------------
function deleteProp(obj, prop){
console.log(obj,prop)
//get own properties
var ownprop = Object.getOwnPropertyNames(obj);
for(var i=0 ;i <ownprop.length; i++){
if (prop === ownprop[i]){
delete obj.ownprop[i];
}
else{
//get the parent
var parent = Object.getPrototypeOf(obj);
console.log(parent);
while (Object.getPrototypeOf(parent)!== Object.prototype){
//recursive call
deleteProp(parent, prop);
}
}
}
}
You don't really need recursion here--a simple while loop will suffice.
function deleteProp(obj, prop) {
while (obj && obj !== Object.prototype) {
delete obj[prop];
obj = Object.getPrototypeOf(obj);
}
}
The check for obj is necessary because an object may have no prototype, for instance if it was created with Object.create(null).
Judging from the output, this should do what you want.
var pro = {'a':1};
var pro2 = Object.create(pro);
var pro3 = Object.create(pro2);
function deleteProp(obj, prop) {
do {
delete obj[prop];
}
while ((obj = Object.getPrototypeOf(obj)));
}
console.log('before', pro.a, pro2.a, pro3.a);
deleteProp(pro3, 'a');
console.log('after', pro.a, pro2.a, pro3.a);
Edit: will this do the trick?

Insert an object into unknow object path

I want to insert an object into a somewhat predefined object:
var obj = {
"scripts": {
"libs":{}
},
"plugins":{}
}
//....
function addobj(path, obj){
console.log(path); // Object {libs: Object}..
path.push(obj); // TypeError: undefined is not a function
}
// Test cases:
addobj(obj["scripts"],{"test":{}});
console.log(obj);
But an error occurs: TypeError: undefined is not a function Why is this happening?
http://jsfiddle.net/Qn3Tb/
Using jQuery, you can use $.extend():
demo
$.extend(path,obj);
You can't .push onto an Object. An Object is a key-value store, therefore you need to assign a key to the object (value) you want to store on the parent object. How you go about achieving that is another question, but something like this might work:
function addobj(path, obj, key) {
path[key || "unnamed"] = obj;
}
If you wanted to add libs to scripts you would do the following:
addobj(script, libs, "libs");
However given what this addobj method actually does, my suggestion would be to drop the abstraction altogether, it's not needed.
Why not simply do
function addProp(prop, value, targetObject){
targetObject[prop] = value;
}
addProp('scripts', { test:{}}, obj);
Based on your question, you can use this to target a specific property:
var obj = {
"scripts": {
"libs":{
"labs": {
foo: 1
}
}
},
"plugins":{}
};
function setPropByString(obj, propString, value) {
if (!propString)
return obj;
var prop, props = propString.split('.');
for (var i = 0, iLen = props.length - 1; i < iLen; i++) {
prop = props[i];
var candidate = obj[prop];
if (candidate !== undefined) {
obj = candidate;
} else {
break;
}
}
obj[props[i]] = value;
}
setPropByString(obj, 'scripts.libs.labs', { something: 1 });
console.log(obj);
Note that this will overwrite the existing prop. So it's propably easier to just extend with jQuery like #A.Wolff suggest.
http://jsfiddle.net/Mn45R/
You cannot do this in the way mentioned in the question.
I believe you should create a function, like
function Node(key) {
var currentNode = this;
this.getKey = function() {
return key;
};
var children = [];
this.addNode(childKey) {
children[children.length] = new Node(childKey);
}
this.search(searchKey) {
if (searchKey === key) {
return currentNode;
}
for (var childIndex in children) {
var searchResult = children[childIndex].search(searchKey);
if (!!searchResult) {
return searchResult;
}
}
return null;
}
}
You can create your root this way:
var root = new Node();
You can add a child to the root this way:
root.addNode("scripts");
This function can be used to add some node to another node having a key
function addNodeToTree(tree, key, newKey) {
var node = tree.search(key);
if (!!node) {
node.addNode(new Node(newKey));
return true;
}
return false;
}
Finally, you can add a node like this:
addNodeToTree(root, "scripts", "test");

how to get the content of java script object in another object?

how to get the content of java script object in another object ?
let say my variable are like follows :
var credentials ={
"name":userName,
"passwd":password
}
var params={
"Credentials":credentials
}
am passing params as an parameter to same other function.In that function i have another object pkt ,as follows :
var pkt={
"name":xxx,
//XXXX
}
what to code at XXXX so that my final pkt structure should be like:
pkt={
"name":xxx,
"Credentials": {
"name":userName,
"passwd":password
}
}
we may have multiple objects inside params,the requirement is that the key value pair should come accordingly.
the equivalent java code is as follows:
Iterator iterKeys = params.keySet().iterator();
while (iterKeys.hasNext())
{
String key = (String)iterKeys.next();
JSONValue value = params.get(key);
pkt.put(key, value);
}
Thanks.
Javascript objects are just Hashmaps.
var credentials = {
"name": "userName",
"passwd": "password"
}
var params = {
"Credentials": credentials
}
var pkt = {
"name": "xxx",
}
for (var property in params) {
if (params.hasOwnProperty(property)) {
var value = property;
pkt[property] = params[property];
}
}
alert(JSON.stringify(pkt));
You can just assign as seen in [this fiddle]
(http://jsfiddle.net/TfWMy/)
You could use a library function like $.extend or _.extend:
pkt = $.extend(pkt, params);
Otherwise you can loop through params and add each key/value pair to pkt:
for (var key in params){
pkt[key] = params[key];
}
Use hasOwnProperty to avoid looping over ancestor members.
You can also use associative array structure.
i.e
var credentials = {
"name": "userName",
"passwd": "password"
}
var pkt = {
"name": "xxx",
}
pkt["Credentials"] = credentials
Not particularly sure about this, but this should work as expected.
Here's two quick functions that should do what you're looking for. The first adds the keys and values to a new object, the second function adds the keys and values to the first object.
var mergeObjectsToNew = function(o1, o2) {
var r = {};
for (var key in o1) {
r[key] = o1[key];
}
for (var key in o2) {
r[key] = o2[key];
}
return r;
}
var mergeObjects = function(o1, o2) {
for (var key in o2) {
o1[key] = o2[key];
}
return o1;
}
I think you are looking to extend your object with another.
Please follow the code and I hope this is the thing you are require.
var credentials ={
"name":"",
"passwd":""
}
var params={
"Credentials":credentials
}
var pkt={
name:"ABC"
};
function Extends(param1,param2){
var object = $.extend({}, param1, param2);
console.log(object);
}
Extends(params,pkt);
Please find Fiddle Below
Fiddle

How to stringify inherited objects to JSON?

json2.js seems to ignore members of the parent object when using JSON.stringify(). Example:
require('./json2.js');
function WorldObject(type) {
this.position = 4;
}
function Actor(val) {
this.someVal = 50;
}
Actor.prototype = new WorldObject();
var a = new Actor(2);
console.log(a.position);
console.log(JSON.stringify(a));
The output is:
4
{"someVal":50}
I would expect this output:
4
{"position":0, "someVal":50}
Well that's just the way it is, JSON.stringify does not preserve any of the not-owned properties of the object. You can have a look at an interesting discussion about other drawbacks and possible workarounds here.
Also note that the author has not only documented the problems, but also written a library called HydrateJS that might help you.
The problem is a little bit deeper than it seems at the first sight. Even if a would really stringify to {"position":0, "someVal":50}, then parsing it later would create an object that has the desired properties, but is neither an instance of Actor, nor has it a prototype link to the WorldObject (after all, the parse method doesn't have this info, so it can't possibly restore it that way).
To preserve the prototype chain, clever tricks are necessary (like those used in HydrateJS). If this is not what you are aiming for, maybe you just need to "flatten" the object before stringifying it. To do that, you could e.g. iterate all the properties of the object, regardless of whether they are own or not and re-assign them (this will ensure they get defined on the object itself instead of just inherited from the prototype).
function flatten(obj) {
var result = Object.create(obj);
for(var key in result) {
result[key] = result[key];
}
return result;
}
The way the function is written it doesn't mutate the original object. So using
console.log(JSON.stringify(flatten(a)));
you'll get the output you want and a will stay the same.
Another option would be to define a toJSON method in the object prototype you want to serialize:
function Test(){}
Test.prototype = {
someProperty: "some value",
toJSON: function() {
var tmp = {};
for(var key in this) {
if(typeof this[key] !== 'function')
tmp[key] = this[key];
}
return tmp;
}
};
var t = new Test;
JSON.stringify(t); // returns "{"someProperty" : "some value"}"
This works since JSON.stringify searches for a toJSON method in the object it receives, before trying the native serialization.
Check this fiddle: http://jsfiddle.net/AEGYG/
You can flat-stringify the object using this function:
function flatStringify(x) {
for(var i in x) {
if(!x.hasOwnProperty(i)) {
// weird as it might seem, this actually does the trick! - adds parent property to self
x[i] = x[i];
}
}
return JSON.stringify(x);
}
Here is a recursive version of the snippet #TomasVana included in his answer, in case there is inheritance in multiple levels of your object tree:
var flatten = function(obj) {
if (obj === null) {
return null;
}
if (Array.isArray(obj)) {
var newObj = [];
for (var i = 0; i < obj.length; i++) {
if (typeof obj[i] === 'object') {
newObj.push(flatten(obj[i]));
}
else {
newObj.push(obj[i]);
}
}
return newObj;
}
var result = Object.create(obj);
for(var key in result) {
if (typeof result[key] === 'object') {
result[key] = flatten(result[key]);
}
else {
result[key] = result[key];
}
}
return result;
}
And it keeps arrays as arrays. Call it the same way:
console.log(JSON.stringify(flatten(visualDataViews)));
While the flatten approach in general works, the snippets in other answers posted so far don't work for properties that are not modifiable, for example if the prototype has been frozen. To handle this case, you would need to create a new object and assign the properties to this new object. Since you're just stringifying the resulting object, object identity and other JavaScript internals probably don't matter, so it's perfectly fine to return a new object. This approach is also arguably more readable than reassigning an object's properties to itself, since it doesn't look like a no-op:
function flatten(obj) {
var ret = {};
for (var i in obj) {
ret[i] = obj[i];
}
return ret;
}
JSON.stringify takes three options
JSON.stringify(value[, replacer[, space]])
So, make use of the replacer, which is a function, that is called recursively for every key-value-pair.
Next Problem, to get really everything, you need to follow the prototpes and you must use getOwnPropertyNames to get all property names (more than you can catch with keysor for…in):
var getAllPropertyNames = () => {
const seen = new WeakSet();
return (obj) => {
let props = [];
do {
if (seen.has(obj)) return [];
seen.add(obj);
Object.getOwnPropertyNames(obj).forEach((prop) => {
if (props.indexOf(prop) === -1) props.push(prop);
});
} while ((obj = Object.getPrototypeOf(obj)));
return props;
};
};
var flatten = () => {
const seen = new WeakSet();
const getPropertyNames = getAllPropertyNames();
return (key, value) => {
if (value !== null && typeof value === "object") {
if (seen.has(value)) return;
seen.add(value);
let result = {};
getPropertyNames(value).forEach((k) => (result[k] = value[k]));
return result;
}
return value;
};
};
Then flatten the object to JSON:
JSON.stringify(myValue, flatten());
Notes:
I had a case where value was null, but typeof value was "object"
Circular references must bee detected, so it needs seen

Do I have to initialize every level of an object in Javascript?

I'm not terribly good with Javascript so I'm wondering if there is a better way of doing this:
if (games[level] === undefined) {
games[level] = {};
games[level]['pending'] = {};
}
if (!games[level]['pending'].length) {
return game.create(level);
}
In PHP I can just test empty($games[$level]['pending']). Is there a better way of testing for this? Basically all I want to do is create the object if it does not exist.
if (games[level] === undefined) {
games[level] = game.create(level);
}
If there is no such level game create should be called to initialize all of the data needed. I don`t see any point of making it an object and then checking for "pending". It will be always empty, because you just created the object.
If your the second if returns something for games[level]['pending'].length you have a big problem with your code. You can`t create an empty object ( games[level]['pending'] = {} ) and find that it already has properties.
In addition:
games[level] = {};
// games[level]['pending'] = {}; - bad
games[level].pending = {}; // this way object properties should be used
you can make yourself a function to do that, pass it the games object a a string like "level.pending.id.something.something" and goes on and creates the objects.
function makeObj(obj, path) {
var parts = path.split("."), tmp = obj, name;
while (parts.length) {
name = parts.shift();
if (typeof tmp[name] === 'undefined') {
tmp[name] = {};
}
tmp = tmp[name];
}
}
var x = {};
makeObj(x, "this.is.a.test");
games[level] = games[level] || {pending: {}};
if (!games[level].pending.length) {
return game.create(level);
}

Categories

Resources