Dave's Brain

Browse - Programming Tips - Using jQuery how can I clone an object?

Date: 2017nov17
Language: javaScript
Library: jQuery
Keywords: copy, duplicate

Q.  Using jQuery how can I clone an object?

A.  Use jQuery's extend() like this:

function shallowClone(obj) { return $.extend({}, obj); }
function deepClone(obj) { return $.extend(true, {}, obj); }
function clone(obj) { // A reasonable default return deepClone(obj); }
Example use:
let newObj = clone(oldObj);

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.