Original: {{ items }}
Sorted: {{ sortedItems }}
Reversed: {{ reversedItems }}
```
## Mutating vs Non-Mutating Array Methods
| Mutating (Avoid in Computed) | Non-Mutating (Safe) |
|------------------------------|---------------------|
| `sort()` | `toSorted()` (ES2023) |
| `reverse()` | `toReversed()` (ES2023) |
| `splice()` | `toSpliced()` (ES2023) |
| `push()` | `concat()` |
| `pop()` | `slice(0, -1)` |
| `shift()` | `slice(1)` |
| `unshift()` | `[item, ...array]` |
| `fill()` | `map()` with new values |
## ES2023 Non-Mutating Alternatives
Modern JavaScript (ES2023) provides non-mutating versions of common array methods:
```javascript
// These return NEW arrays, safe for computed properties
const sorted = array.toSorted((a, b) => a - b)
const reversed = array.toReversed()
const spliced = array.toSpliced(1, 2, 'new')
const withReplaced = array.with(0, 'newFirst')
```
## Deep Copy for Nested Arrays
For arrays of objects where you might mutate nested properties:
```javascript
const items = ref([{ name: 'A', values: [1, 2, 3] }])
// Shallow copy - nested arrays still shared
const copied = computed(() => [...items.value])
// Deep copy if you need to mutate nested structures
const deepCopied = computed(() => {
return JSON.parse(JSON.stringify(items.value))
// Or use structuredClone():
// return structuredClone(items.value)
})
```
## Reference
- [Vue.js Computed Properties - Avoid Mutating Computed Value](https://vuejs.org/guide/essentials/computed.html#avoid-mutating-computed-value)
- [MDN Array Methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)