我开始用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];
}
}
但这表示它有“空位”:
数组[“数学”,&lt; 1空插槽&gt;,“英语”,“科学”,&lt; 1空插槽&gt;,“体育”,&lt; 1空插槽&gt;,“英语”,“科学” ,“数学”,还有4个......]
但它应该不再存在了。我怎么能这样做?
答案 0 :(得分:1)
正如您已经发现的那样,数组可能是稀疏的&#34; (并非所有索引都必须具有值)以及您使用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);
&#13;
答案 1 :(得分:1)
使用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' ]
答案 2 :(得分:1)
试一试:
for (var index in subjects){
if (subjects[index] == "Spanish"){
subjects.splice(index,1);
}
if (subjects[index] == "History"){
subjects.splice(index,1);
}
}
答案 3 :(得分:0)
var index = array.indexOf('history');
if (index > -1) {
array.splice(index, 1);
}
splice的第二个参数是要删除的元素数。请注意,splice会修改数组并返回一个包含已删除元素的新数组。
答案 4 :(得分:0)
正如您所知,删除操作符只是删除数组中某个位置的项目。这个位置仍然存在,但是该空间中的内容现在只是空的(数组可以有空格)。见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);
答案 5 :(得分:0)
你可以这样做:
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");