如何从 JavaScript 数组中删除项目?
How do I remove items from a JavaScript Array?
我开始用 JavaScript 学习编码,我们的老师让我们搜索这个网站。我的东西没有答案所以我想问一下。我希望没问题,我已经搜索了很多问题,但找不到任何与我的问题相似的内容。
我的任务是检查我的时间表并找出我最不喜欢的两个科目。这是我的:
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
我说我想选修西班牙语和历史,我做到了:
for (var i = 0; i < 14; i++) {
if (subjects[i] == "Spanish") {
delete subjects[i];
}
if (subjects[i] == "History") {
delete subjects[i];
}
}
但这表示它有 "empty slots" :
Array [ "Maths", <1 empty slot>, "English", "Science", <1 empty slot>, "Physical Education", <1 empty slot>, "English", "Science", "Maths", 4 more… ]
但它不应该再在那里了。我该怎么做?
正如您所发现的,数组可以是 "sparse"(并非所有索引都必须有值),而这正是您使用 delete
完成的。您删除了数据,但保留了索引。
要完全删除索引,请使用 .splice()
method:
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
for (var i = 0; i < 14; i++) {
if (subjects[i] == "Spanish") {
subjects.splice(i, 1); // At the current index, remove one element
}
if (subjects[i] == "History") {
subjects.splice(i, 1); // At the current index, remove one element
}
}
console.log(subjects);
var index = array.indexOf('history');
if (index > -1) {
array.splice(index, 1);
}
splice的第二个参数是要移除的元素个数。请注意,splice 会就地修改数组,returns 一个包含已删除元素的新数组。
使用splice:
var subjects = ["Maths", "History", "English", "Science", "Spanish", "Physical Education", "History", "English", "Science", "Maths", "History", "English", "Spanish", "Physical Education"];
function remove(arr, item) {
for(var i = arr.length; i--;) {
if(arr[i] === item) {
arr.splice(i, 1);
}
}
}
remove(subjects, "Spanish");
remove(subjects, "History");
document.write(subjects);
输出:
[ 'Maths',
'English',
'Science',
'Physical Education',
'English',
'Science',
'Maths',
'English',
'Physical Education' ]
如您所知,删除运算符只是删除数组中某个位置的项目。这个位置仍然存在,但是 space 中的内容现在只是空的(数组可以有空的 space)。参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Deleting_array_elements
您可以使用拼接(参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)按索引位置删除项目。
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
for (var i = 0; i < subjects.length; i++) {
if (subjects[i] === "Spanish") {
subjects.splice(i, 1);
}
else if (subjects[i] === "History") {
subjects.splice(i, 1);
}
}
console.log(subjects);
或者您可以使用过滤器(参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter):
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
var isSubjectILike = function(subject){
return subject !== "Spanish" && subject !== "History";
};
subjects = subjects.filter(isSubjectILike);
console.log(subjects);
你可以这样做:
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
var index = subjects.indexOf("Spanish");
subjects = Object.assign(subjects.slice(0, index), subjects.slice(index + 1, subjects.length -1);
或者使用 ES6 过滤器:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
subjects = subjects.filter((value)=> value!="Spanish");
试一试:
for (var index in subjects){
if (subjects[index] == "Spanish"){
subjects.splice(index,1);
}
if (subjects[index] == "History"){
subjects.splice(index,1);
}
}
我开始用 JavaScript 学习编码,我们的老师让我们搜索这个网站。我的东西没有答案所以我想问一下。我希望没问题,我已经搜索了很多问题,但找不到任何与我的问题相似的内容。
我的任务是检查我的时间表并找出我最不喜欢的两个科目。这是我的:
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
我说我想选修西班牙语和历史,我做到了:
for (var i = 0; i < 14; i++) {
if (subjects[i] == "Spanish") {
delete subjects[i];
}
if (subjects[i] == "History") {
delete subjects[i];
}
}
但这表示它有 "empty slots" :
Array [ "Maths", <1 empty slot>, "English", "Science", <1 empty slot>, "Physical Education", <1 empty slot>, "English", "Science", "Maths", 4 more… ]
但它不应该再在那里了。我该怎么做?
正如您所发现的,数组可以是 "sparse"(并非所有索引都必须有值),而这正是您使用 delete
完成的。您删除了数据,但保留了索引。
要完全删除索引,请使用 .splice()
method:
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
for (var i = 0; i < 14; i++) {
if (subjects[i] == "Spanish") {
subjects.splice(i, 1); // At the current index, remove one element
}
if (subjects[i] == "History") {
subjects.splice(i, 1); // At the current index, remove one element
}
}
console.log(subjects);
var index = array.indexOf('history');
if (index > -1) {
array.splice(index, 1);
}
splice的第二个参数是要移除的元素个数。请注意,splice 会就地修改数组,returns 一个包含已删除元素的新数组。
使用splice:
var subjects = ["Maths", "History", "English", "Science", "Spanish", "Physical Education", "History", "English", "Science", "Maths", "History", "English", "Spanish", "Physical Education"];
function remove(arr, item) {
for(var i = arr.length; i--;) {
if(arr[i] === item) {
arr.splice(i, 1);
}
}
}
remove(subjects, "Spanish");
remove(subjects, "History");
document.write(subjects);
输出:
[ 'Maths',
'English',
'Science',
'Physical Education',
'English',
'Science',
'Maths',
'English',
'Physical Education' ]
如您所知,删除运算符只是删除数组中某个位置的项目。这个位置仍然存在,但是 space 中的内容现在只是空的(数组可以有空的 space)。参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Deleting_array_elements
您可以使用拼接(参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)按索引位置删除项目。
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
for (var i = 0; i < subjects.length; i++) {
if (subjects[i] === "Spanish") {
subjects.splice(i, 1);
}
else if (subjects[i] === "History") {
subjects.splice(i, 1);
}
}
console.log(subjects);
或者您可以使用过滤器(参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter):
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
var isSubjectILike = function(subject){
return subject !== "Spanish" && subject !== "History";
};
subjects = subjects.filter(isSubjectILike);
console.log(subjects);
你可以这样做:
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
var index = subjects.indexOf("Spanish");
subjects = Object.assign(subjects.slice(0, index), subjects.slice(index + 1, subjects.length -1);
或者使用 ES6 过滤器:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
var subjects = [
"Maths", "History", "English", "Science", "Spanish",
"Physical Education", "History", "English", "Science",
"Maths", "History", "English", "Spanish", "Physical Education"
];
subjects = subjects.filter((value)=> value!="Spanish");
试一试:
for (var index in subjects){
if (subjects[index] == "Spanish"){
subjects.splice(index,1);
}
if (subjects[index] == "History"){
subjects.splice(index,1);
}
}