Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a ReadonlyArray<any> to a usual mutable array []?

In some cases Angular functions and callbacks return a ReadonlyArray. But array should be used as usual one, as I don't use some immutable structures like ones in Redux.

So how can I convert ReadonlyArray to a javascript native array?

like image 778
Kach Avatar asked Nov 21 '19 18:11

Kach


People also ask

What is a mutable array?

A mutable value is one that can be changed without creating an entirely new value. In JavaScript, objects and arrays are mutable by default, but primitive values are not — once a primitive value is created, it cannot be changed, although the variable that holds it may be reassigned.

How do you make an array immutable in TypeScript?

To make an array immutable or read-only, one of the straightforward ways to achieve is to use the as const syntax after the array itself in TypeScript.

Are arrays mutable in C?

So, are arrays mutable or immutable? In terms of mutability, we say that plain C arrays are immutable because the structure itself cannot change once it has been created.

What is ReadonlyArray?

The ReadonlyArray type describes Array s that can only be read from. Any variable with a reference to a ReadonlyArray can't add, remove, or replace any elements of the array. function foo(arr: ReadonlyArray<string>) { arr.


1 Answers

In this case .concat() or .slice() without parameters can be used. they create new array based on immutable one. It is kind of shallow copy. Something similar: How can I Convert a ReadonlyArray<any> to any[]?

like image 154
Kach Avatar answered Oct 20 '22 04:10

Kach