logo
  • Home
  • Contact
💵
Should I be buying I-Bonds in 2023?
💰
Need a step-by-step guide to opening your Roth IRA?
Buy me a coffee Buy me a coffee
aws c++ career cli css docker finance git java js misc python sql

How to Make a Shallow Copy of Array and Object in JavaScript


Modern JavaScript has made shallow copying easier than ever. ES6 (ES2015) introduced the spread operator ..., which essentially dumps the contents of an array or object.

Copy an Array

const original = [1, 2, 3];
const copy = [ ...original ];

Copy an Object

const original = { 
  key1: 1,
  key2: 2
};
const copy = { ...original };

Related in JS

How to Validate a UUID with Regular Expressions in JavaScript
How to Print All Properties of an Object to JSX in React.js
How to Allow target="blank" in DOMPurify.sanitize()
How to Replace All URLs in a String with Links (Anchor Tags) in JavaScript
How to Remove the Last Character from a String in JavaScript
How to Add Script Tag to HTML DOM from JavaScript
How to Get a Website User's Location in JavaScript
How to Convert Array of Objects to Hash Map By Key in JavaScript
How to Check if Dates are on the Same Day in JavaScript
How to Get User ID from Session in NextAuth (with or without JWTs)
How to Insert To and Delete From Arrays in Prisma
How to Create a Tooltip using TailwindCSS in JavaScript
How to Redirect Page from getServerSideProps or getStaticProps in Next.js
How to Add Google Analytics to a Next.js Application (including TypeScript)
How to Test JavaScript Web Applications on Mobile Without Deploying (in 1 minute!)
How to Add Placeholder to Listbox Select Element in Headless UI
© , LogFetch   âˆ™   Advertise Here   âˆ™   Privacy Policy