Skip to content
nive.sh

Loading index…

↑↓ navigate · ↵ opennive.sh

A Practical guide to use Arrays and Objects in JavaScript

JavaScript objects and arrays are fundamental data structures used to organize and store data. In this comprehensive guide, we will explore the concepts of objects and arrays, provide relatable examples for both technical and non-technical individuals, and delve into safe techniques for copying and modifying these data structures. By following the discussed techniques, you can ensure data integrity, avoid unintended side effects, and confidently work with JavaScript data.

Nivesh Saharan

· 4 min read · 624 words

Introduction

JavaScript objects and arrays are fundamental data structures used to organize and store data. In this comprehensive guide, we will explore the concepts of objects and arrays, provide relatable examples for both technical and non-technical individuals, and delve into safe techniques for copying and modifying these data structures. By following the discussed techniques, you can ensure data integrity, avoid unintended side effects, and confidently work with JavaScript data.

Understanding Objects and Arrays

Objects and arrays are containers that allow us to group related data together. They provide a way to organize and access information efficiently. Let's take a closer look at each of them with relatable examples.

Objects

An object can be thought of as a real-life item that possesses certain characteristics or attributes. For example, let's consider a person named John. We can represent John as an object with various properties, such as his name, age, and occupation.

const person = {
  name: "John",
  age: 25,
  occupation: "Software Developer"
};

In this example, person is an object that represents John. The properties (name, age, and occupation) describe different aspects of John's identity.

Arrays

An array is like a list—a collection of items in a specific order. Imagine a shopping list with items like apples, bread, and milk. Each item represents an element in the array, and the order of items is crucial.

const shoppingList = ["apples", "bread", "milk"];

Here, shoppingList is an array that holds the items we want to buy. The elements are stored in a particular sequence, allowing us to access them based on their position or index.

Copying and Modifying Arrays Safely

To create a copy of an array without modifying the original, we can use safe techniques that preserve the array's structure.

Shallow Copy

A shallow copy creates a new array with references to the same elements. We can achieve this using the slice() method.

const originalArray = [1, 2, 3];
const newArray = originalArray.slice();

Deep Copy

For a deep copy that ensures independence from the original array, even with nested objects or arrays, we can use JSON.parse(JSON.stringify(originalArray)).

const originalArray = [1, [2, 3], { name: "John" }];
const deepCopyArray = JSON.parse(JSON.stringify(originalArray));

Updating Arrays Safely

Modifying the copied array won't affect the original, ensuring data integrity.

const originalArray = [1, 2, 3];
const newArray = originalArray.slice();
newArray[0] = 10;

Copying and Modifying Objects Safely

Copying objects requires caution, as they are assigned by reference. However, we can employ safe techniques to create independent copies.

Shallow Copy

To create a shallow copy of an object, we can use Object.assign() or the spread operator ({...}).

const originalObject = { name: "John", age: 25 };
const newObject = Object.assign({}, originalObject);
// or const newObject = { ...originalObject };

Deep Copy

For a deep copy, including nested objects, we can use JSON.parse(JSON.stringify(originalObject)).

const originalObject = { name: "John", data: { age: 25 } };
const deepCopyObject = JSON.parse(JSON.stringify(originalObject));

Updating Objects Safely

By modifying properties within the copied object, we prevent unintended changes to the original object.

const originalObject = { name: "John", age: 25 };
const newObject = Object.assign({}, originalObject);
newObject.age = 30;

Conclusion

Objects and arrays are essential data structures in JavaScript, facilitating effective organization and management of data. By employing safe techniques for copying and modifying these structures, such as using slice() for arrays, Object.assign() or the spread operator for shallow object copies, and JSON.parse(JSON.stringify()) for deep object copies, you can ensure data integrity and avoid unintended side effects. Whether you're a beginner or an experienced developer, understanding and implementing these techniques will empower you to confidently work with JavaScript data structures, maintaining control over your data.

Keep reading

Related articles

All articles

Jun 27, 2023 · 5 min read

Arrays and Tuples

There are two primitive compound types in Rust: Arrays and Tuples.

  • JavaScript
  • Rust

Jun 23, 2023 · 3 min read

Hello, Rust!

Welcome to Rust for JavaScript Developers! It's fantastic to have you here. Let's dive right in.

  • JavaScript
  • Rust

Jun 23, 2023 · 3 min read

Scalars

Rust is a statically typed programming language. This means that all values must have a known type at compile time. So how did we get away without adding annotations in our Hello, Rust example? The Rust compiler is quite excellent at type inference and will often infer types for your values and only ask you to specify a type if it can't.

  • JavaScript
  • Rust

Newsletter

Build notes, when there is something worth sending.

New deep-dive articles and the occasional note on what I am shipping. No schedule, no filler, unsubscribe any time.

Taking on new projects

Need help with custom web and mobile engineering?

Tell me about the storefront, product or platform you’re working on. A 30-minute call is usually enough to map the first steps.

Email

hey@nive.sh

Local time

--:-- · Gurugram (IST)