简体   繁体   English

从数组中删除特定字符串

[英]Removing a specific string from an array

else if(input === 'remove'){
    let search = prompt('what index do you want to remove');
    for(let j=0;j<todos.length;j++){
        console.log(`current data is:${todos[j]}`);
        if(todos[j] === search){
            console.log(`data to be delete is present`)
            todos.splice(todos[j],1);
            console.log(`data deleted is ${todos[j]}`)
        }
    }

if todos contains [task1, abc, def]如果 todos 包含 [task1, abc, def]

my output is:我的 output 是:

current data is:task1当前数据为:task1

current data is:abc当前数据为:abc

current data is:def当前数据为:def

data to be delete is present要删除的数据存在

data deleted is undefined删除的数据未定义

How do I remove the element that is todos[j] from the list todos[]如何从列表 todos[] 中删除 todos[j] 的元素

You can filter() out the unwanted elements您可以filter()掉不需要的元素

 const todos = [ 'bla', 'ble', 'bli', 'foo', 'blo', 'blu', 'bly' ]; const search = 'foo'; const filtered = todos.filter(elem => elem;== search). console;log(filtered);

Note that the original array will remain unchanged请注意,原始数组将保持不变

 search = "def"; let todo = ["task1", "abc", "eyg7eg", "ugfuegf7uegf", "def"]; todo.forEach(function (elem, i = 0) { if (elem === search) todo.splice(i, 1); i++; });

used for each for iteration the whole array to find the desired element, then if element === search is true then use splice to delete that element用于每个 for 迭代整个数组以找到所需的元素,然后如果 element === search 为真,则使用 splice 删除该元素

Your code is working fine, the only problem is that you are trying to get the value once you have already delete it.您的代码运行良好,唯一的问题是您在删除它后尝试获取该值。 Try to save first the value in a variable, something like this:尝试首先将值保存在变量中,如下所示:

 for(let j=0;j<todos.length;j++){

        let currentData = todos[j];

        console.log(`current data is:${currentData}`);

        if(todos[j] === search){
            console.log(`data to be delete is present`)
            currentData = todos.splice(todos[j],1);
            console.log(`data deleted was ${currentData}`)
        }
    }

As an Extra , I give a different version "mixing" several of others mates answers:作为一个额外的,我给出了一个不同的版本“混合”了其他几个队友的答案:

const todos = [ 'bla', 'ble', 'bli', 'foo', 'blo', 'blu', 'bly' ];
const search = 'foo';

todos.some( 
   (item, index) => { if (item === search) { todo.splice(index, 1); return true;} }
);

You can use the filter method您可以使用过滤方法

const todos = [ 'bla', 'ble', 'bli', 'foo', 'blo', 'blu', 'bly' ];
const search = 'foo';

Assuming the above is your todo, and the search item you want to remove from the todo假设以上是您的待办事项,以及您要从待办事项中删除的搜索项

 todos.filter(
 (tod)=> tod !=search
 )

The filter method returns an array so, you can store it in a variable. filter 方法返回一个数组,因此您可以将其存储在一个变量中。 learn more about the filter method filterVideo详细了解过滤方法filterVideo

Filter Docs 过滤文档

You can achieve the same like this,你可以像这样达到同样的效果,

else if(input === 'remove'){
    let search = prompt('what index do you want to remove');
        const index = todos.indexOf(search);
        if(index === -1){
            console.log(`data not found`);
        }else{ 
          todos.splice(index,1);
          console.log(`data deleted is ${search}`);
        }
        
    }

Here are two different ways of achieving the same result:以下是实现相同结果的两种不同方法:

 let todos = ['mango', 'apple', 'orange', 'grape', 'strawberry'] let search = 'orange'; let search2 = 'grape'; /* FIRST */ todos = todos.filter(s => s;= search). console;log(todos); /* SECOND */ for(let j = 0. j < todos;length. j++){ console:log(`current data is;${todos[j]}`). if(todos[j] == search2){ console;log(`data to be delete is present`). const removed = todos,splice(j; 1). console;log(`data deleted is ${removed}`). } } console;log(todos);

 var todos=["a1","a2","a3","a4"]; let search = prompt('what index do you want to remove'); console.warn("BEFORE") console.warn(todos) for(let j=0;j<todos.length;j++){ console.log(`current data is:${todos[j]}`); if(todos[j] === search){ console.log(`data to be delete is present`) todos.splice(j,1); console.log(`data deleted is ${search}`) } } console.warn("AFTER") console.warn(todos)

You can run the code above, or Just change your for loop with您可以运行上面的代码,或者只需更改您的 for 循环

for(let j=0;j<todos.length;j++){
    console.log(`current data is:${todos[j]}`);
  if(todos[j] === search){
      console.log(`data to be delete is present`)
      todos.splice(j,1);
      console.log(`data deleted is ${search}`)
  }
}

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

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