Search⌘ K
AI Features

Preserving the Order in Arrays

Explore how to maintain the order of items in Redux state objects by using arrays and objects with ID references. Understand why preserving order is crucial for applications like chat apps, and discover techniques to keep your state organized and reliable.

We'll cover the following...

Caveat #2 : Preservation of Order.

This is perhaps the number one reason people use arrays. Arrays preserve the order of their values.

You have to see an example to understand this.

const numbers = [0,3,1,6,89,5,7,9]

Whatever you do, fetching the value of numbers will always return the same array, with the order of the inputs unaltered.

How about an object? Try it out below.

Node.js
const numbers = {
0: "Zero",
3: "Three",
1: "One",
6: "Six",
89: "Eighty-nine",
5: "Five",
7: "Seven",
9: "Nine"
};
console.log(numbers);

The order of the object values aren’t returned in the same way!

Now, depending on the kind of ...