简体   繁体   English

如何从JavaScript数组中删除项目?

[英]How do I remove items from a JavaScript Array?

I am starting to learn coding with JavaScript and our teacher told us to search this site. 我开始用JavaScript学习编码,我们的老师告诉我们要搜索这个网站。 There is no answer for my stuff so I wanted to ask. 我的东西没有答案所以我想问。 I hope that is OK, I have searched a lot of questions already but I can't find anything that is like my question. 我希望没关系,我已经搜索了很多问题,但我找不到任何像我的问题。

My task is to go through my timetable and take out my two least favourite subject. 我的任务是完成我的时间表并拿出我最不喜欢的两个主题。 This is what I have: 这就是我所拥有的:

var subjects = [
  "Maths", "History", "English", "Science", "Spanish", 
  "Physical Education", "History", "English", "Science", 
  "Maths", "History", "English", "Spanish", "Physical Education"
];

I said I wanted to take out Spanish and History and I did that: 我说我想拿出西班牙语和历史,我这样做了:

for (var i = 0; i < 14; i++) {
   if (subjects[i] == "Spanish") {
   delete subjects[i];
  }
  if (subjects[i] == "History") {
    delete subjects[i];
  }
}

But this says it has "empty slots" : 但这说它有“空位”:

Array [ "Maths", <1 empty slot>, "English", "Science", <1 empty slot>, "Physical Education", <1 empty slot>, "English", "Science", "Maths", 4 more… ] 数组[“数学”,<1空插槽>,“英语”,“科学”,<1空插槽>,“体育”,<1空插槽>,“英语”,“科学”,“数学”,4更多… ]

But it should simply not be in there anymore. 但它应该不再存在于那里。 How can I do that? 我怎样才能做到这一点?

As you've found out, arrays can be "sparse" (not all indexes must have a value) and that's what you've accomplished with delete . 正如您所知,数组可以是“稀疏的”(并非所有索引都必须具有值),这就是您通过delete实现的功能。 You deleted the data, but left the index. 您删除了数据,但保留了索引。

To remove the index completely, use the .splice() method for this: 要完全删除索引,请使用.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); 

Use 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); 

Output: 输出:

[ 'Maths',
  'English',
  'Science',
  'Physical Education',
  'English',
  'Science',
  'Maths',
  'English',
  'Physical Education' ]

Try it: 试试吧:

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);
}

The second parameter of splice is the number of elements to remove. splice的第二个参数是要删除的元素数。 Note that splice modifies the array in place and returns a new array containing the elements that have been removed. 请注意,splice会修改数组并返回一个包含已删除元素的新数组。

As you figured out the delete operator just deletes the item at a location in an array. 正如您所知,删除操作符只是删除数组中某个位置的项目。 This spot is still there, but what was in that space is just empty now (arrays can have empty spaces). 这个位置仍然存在,但是该空间中的内容现在只是空的(数组可以有空格)。 See 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/Operators/delete#Deleting_array_elements

You can use splice (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice ) to remove items by index position. 您可以使用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); 

Or you can use filter (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter ): 或者您可以使用过滤器(请参阅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); 

You could do it like this: 你可以这样做:

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);

Or by using ES6 filter: 或者使用ES6过滤器:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM