`_.clone` function on object or array using JavaScript Library underscore.js ( #shorts )

1 year ago
11

Hi Friends,

`_.clone` function on object or array using JavaScript Library underscore.js

`_.clone` is a utility function in Underscore.js library is commonly used when you want to duplicate an object or array without modifying the original one.

In below example, we have an array originalArray with some elements. We use _.clone to create a shallow copy of the array and assign it to clonedArray. Then, we modify clonedArray by pushing an additional element. As a result, the originalArray remains unchanged, while the clonedArray contains the additional element.

Note: Changes made to the cloned object or array will not affect the original one, and vice versa.

// syntax
_.clone(value)

// example
var originalArray = [1, 2, 3, 4, 5];
var clonedArray = _.clone(originalArray);

clonedArray.push(6);

console.log(originalArray);
// [1, 2, 3, 4, 5]

console.log(clonedArray);
// [1, 2, 3, 4, 5, 6]

#javascript #under #score #underscore #underscorejs #clone #underscoreclone #clonefunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official

Loading comments...