Skip to content

Instantly share code, notes, and snippets.

@arian
Created September 20, 2010 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arian/588002 to your computer and use it in GitHub Desktop.
Save arian/588002 to your computer and use it in GitHub Desktop.
Simple JavaScript function to copy values from one object to another
// Simple JavaScript function to copy values from one object to another
Object.copy = function(from, to, keys){
l = keys.length;
while (l--) to[keys[l]] = from[keys[l]];
return to;
};
var obj1 = {foo: 'bar', temp: 'foo', bar: 'temp'};
obj2 = {};
console.log(Object.copy(obj1, obj2, ['foo', 'bar']));
@ibolmo
Copy link

ibolmo commented Sep 20, 2010

Object.copy = function(object, to, keys){
  to = to || {};
  if (!keys) for (var i in object) to[i] = object[i];
  else for (var i in keys) to[keys[i]] = object[keys[i]];
  return to;
};

:)

@arian
Copy link
Author

arian commented Sep 20, 2010

haha cool! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment