Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript怎么清空数组 #89

Open
Sunny-117 opened this issue Nov 3, 2022 · 4 comments
Open

JavaScript怎么清空数组 #89

Sunny-117 opened this issue Nov 3, 2022 · 4 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@Nasuke
Copy link
Contributor

Nasuke commented Nov 4, 2022

let sheldon = ['c','o','o','p','e','r']
//methods 1
sheldon = []

// methods 2
sheldon.length = 0

// methods 3
sheldon.splice(0, sheldon.length)

// methods 4
while(sheldon.length > 0) sheldon.pop()

//

@ppjiucai
Copy link

// methods 1
let myArray = [1, 2, 3, 4, 5];
myArray.length = 0;
console.log(myArray); // Output: []
// methods 2
let myArray = [1, 2, 3, 4, 5];
myArray.splice(0, myArray.length);
console.log(myArray); // Output: []
// methods 3
arry = [1, 2, 3, 4];
arry = [];
console.log(arry); //Output: []
// method 4
arry = [1, 2, 3, 4];
while (arry.length > 0) {
arry.pop();
}

console.log(arry);
//Output: []
// mehtod 5
arry = [1, 2, 3, 4];
while (arry.length > 0) {
arry.shift();
}

console.log(arry);
//Output: []

@kangkang123269
Copy link

  1. 数组的length属性
var array = [1, 2, 3];
array.length = 0;

2.splice方法

var array = [1, 2, 3];
array.splice(0,array.length);
  1. 新建一个新的空数组覆盖原来的
var array = [1, 2, 3];
array = [];
  1. shift
var array = [1, 2, 3];
while(array.length > 0) {
    array.shift();
}
  1. pop
var array = [1, 2, 3];
while(array.length > 0) {
    array.pop();
}
  1. slice
var array = [1, 2, 3];
array = array.slice(array.length);
  1. filter
var array = [1, 2, 3];
// 7.1
array = array.filter(() => false);
// 7.2
array = array.filter(item => item !== undefined);
  1. fill
var array = [1, 2, 3];
array.fill(undefined);

@liliphoenix
Copy link

array.fill*()

  1. 数组的length属性
var array = [1, 2, 3];
array.length = 0;

2.splice方法

var array = [1, 2, 3];
array.splice(0,array.length);
  1. 新建一个新的空数组覆盖原来的
var array = [1, 2, 3];
array = [];
  1. shift
var array = [1, 2, 3];
while(array.length > 0) {
    array.shift();
}
  1. pop
var array = [1, 2, 3];
while(array.length > 0) {
    array.pop();
}
  1. slice
var array = [1, 2, 3];
array = array.slice(array.length);
  1. filter
var array = [1, 2, 3];
// 7.1
array = array.filter(() => false);
// 7.2
array = array.filter(item => item !== undefined);
  1. fill
var array = [1, 2, 3];
array.fill(undefined);

array.fill(undefined)不可以吧

[ undefined, undefined, undefined ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants