Warning: session_start(): open(/tmp/sess_8dba3b437a71ab8fbb1f8550e1408311, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to empty an array in TypeScript - LearnShareIT

How to empty an array in TypeScript

How to Empty an Array in TypeScript

There are many ways to empty an array in Typescript, like using the splice method or doing it manually. So how to do it? Let’s go into detail now.

Empty an array in TypeScript

Use splice method

The splice will change your origin array by removing elements, and it can also add the element.

Syntax:

array.splice(start, remove amount, add items)

Parameters:

  • start: The start index that you want to remove the element.
  • remove amount: The number of elements you want to remove from your array.
  • add items: All items you want to add to your array replace the removed one. So that if you pass in only the start position is 0, then all elements in the array will be removed.

Example:

const nameArray: string[] = [
    "Tommy",
    "Jonmma",
    "Alex",
    "Anna",
    "Antony",
    "Hallan",
    "Michel",
];

nameArray.splice(0)
console.log(nameArray);

Output:

[LOG]: [] 

As you see here, I have removed all elements in the origin array with the splice method. If you don’t want to change in origin array, you can clone a new array and then work with it.

Example:

const nameArray: string[] = [
    "Tommy",
    "Jonmma",
    "Alex",
    "Anna",
    "Antony",
    "Hallan",
    "Michel",
];

// Clone the new array
const newArr = Object.assign([], nameArray);

// Empty array
newArr.splice(0);

console.log(newArr);
console.log(nameArray);

Output:

[LOG]: []
[LOG]: ["Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel"]

Assign to empty array

You can reassign an array to an empty one, but it might make you lose data, so you should store the value somewhere.

Example:

let nameArray: string[] = [
    "Tommy",
    "Jonmma",
    "Alex",
    "Anna",
    "Antony",
    "Hallan",
    "Michel",
];

// Clone the new array
const newArr = Object.assign([], nameArray);

// Assign to an empty array
nameArray = [];

console.log(newArr);
console.log(nameArray);

Output:

[LOG]: ["Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel"]
[LOG]: []

If you set the length array to 0, your array also becomes empty.

Example:

let nameArray: string[] = [
    "Tommy",
    "Jonmma",
    "Alex",
    "Anna",
    "Antony",
    "Hallan",
    "Michel",
];

// Set the length array to 0
nameArray.length = 0;
console.log(nameArray);

Output:

[LOG]: [] 

Use pop method

The pop method will help you remove the last element in your array so that you can use that pop method in one loop based on your array length.

Syntax:

array.pop()

Example:

let nameArray: string[] = [
    "Tommy",
    "Jonmma",
    "Alex",
    "Anna",
    "Antony",
    "Hallan",
    "Michel",
];

// Use the while loop and pop() method to empty the array
while (nameArray.length) {
    nameArray.pop();
}

console.log(nameArray);

Output:

[LOG]: [] 

Here with a while loop, the loop will continue until the condition return false, which here is 0.

You can read more about array type here.

Summary

In this article, I showed you how to empty an array in TypeScript. I recommend you use the splice method or simply assign it to an empty array if you have no special condition.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *