我开始学习使用 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];
}
}
但这表示它有“空槽”:
Array [“数学”、<1 empty slot>、“英语”、“科学”、<1 empty slot>、“体育”、<1 empty slot>、“英语”、“科学”、“数学”、4 个以上...]
但它不应该再在那里了。我怎样才能做到这一点?
正如您所发现的,数组可以是“稀疏”的(并非所有索引都必须有值),这就是您使用
delete
所实现的。您删除了数据,但保留了索引。
.splice()
方法:
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 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' ]
尝试一下:
for (var index in subjects){
if (subjects[index] == "Spanish"){
subjects.splice(index,1);
}
if (subjects[index] == "History"){
subjects.splice(index,1);
}
}
var index = array.indexOf('history');
if (index > -1) {
array.splice(index, 1);
}
splice的第二个参数是要移除的元素数量。请注意,splice 会就地修改数组并返回一个包含已删除元素的新数组。
你可以这样做:
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");
正如您所发现的,删除运算符只是删除数组中某个位置的项目。这个位置仍然存在,但是那个空间中的内容现在是空的(数组可以有空的空间)。请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Deleting_array_elements
您可以使用 splice(请参阅 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);
我认为更现代的解决方案可能如下所示:
subjects.filter(value => !(['Spanish', 'History'].includes(value)))