Answers for "javascript clear an array"

21

javascript copy an array

let arr =["a","b","c"];
// ES6 way
const copy = [...arr];

// older method
const copy = Array.from(arr);
Posted by: Guest on July-02-2020
13

javascript empty array

arr = [];   // set array=[]

//function
const empty = arr => arr.length = 0;
//example
var arr= [1,2,3,4,5];
empty(arr) // arr=[]
Posted by: Guest on July-02-2020
5

javascript clear array

let aray = [1,2,3,4,5,6,7,8,9,10];

//as with most coding there are several ways you can do anything
//see whichever works best for your scenario

//set the array to equal a blank array
array = [];

//set the array's length to 0
array.length = 0;

//using splice, starts at index 0 & removes everything up to and including that last index 
array.splice(0, array.length);

//map or loop through and shift() or pop();
//goes through the array one at a time and removes the last item each time
array.map( () => array.pop());

//goes through the array one at a time and removes the first item each time
array.map( () => array.shift());
Posted by: Guest on October-30-2020
3

javascript array clear

var cars = ["mazda","honda","tesla"];
	cars = []; // clear/empty the array
Posted by: Guest on October-22-2019
1

how to clear array in javascript

A.length = 0
Posted by: Guest on October-28-2020
0

javascript clear an array

let arr = [3.14, 7];

arr = [];//make sure there is no other reference
Posted by: Guest on February-08-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language