【问题标题】:How to delete an input stored in array in JS如何在JS中删除存储在数组中的输入
【发布时间】:2021-08-28 16:23:07
【问题描述】:
let input;
const todos = [];

while (input !== 'exit') {
    input = prompt('Type what you want to do');
    if (input === 'new') {
        input = prompt("What's your todo?");
        todos.push(input);
    } else if (input === 'list') {
        console.log(`This is your list of todos: ${todos}`);
    } else if (input === 'delete') {
        input = prompt('Which todo would you like to delete?');
        if (todos.indexOf(input) !== -1) {
            todos.splice(1, 1, input);
            console.log(`You've deleted ${input}`);
        }
    } else {
        break;
    }
}

这就是我迄今为止所尝试的。 我开始编程,这是一个小练习的一部分,我必须从提示中要求添加新的待办事项,将其全部列出然后删除。 我要做的是:获取存储在输入变量中的输入,然后检查它是否在数组中,如果它是肯定的,我想删除它而不是从索引中删除它。

喜欢:

-删除 -吃 //检查它是否在数组内部 //如果为真,删除它

如果这是一个愚蠢的问题,我深表歉意。我在网上试了,没找到。

谢谢!

【问题讨论】:

  • 欢迎来到 SO! splice 的工作原理并非如此。请查看文档:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…。这个想法是存储元素的索引并使用它来按索引拼接,而不是为拼接硬编码索引 1。 splice 的第三个参数是您要添加的可选数据,而不是要删除的数据(是的,这是一个奇怪的功能)。

标签: javascript arrays input splice


【解决方案1】:

您的代码固定在下面:

input = prompt('Which todo would you like to delete?');
const index = todos.indexOf(input);
if (~index) {
  todos.splice(index, 1);
  console.log(`You've deleted ${input}`);
}

您可以在相应的 MDN 文档中阅读有关 Array.prototype.splice()bitwise operator 的更多信息。

【讨论】:

    【解决方案2】:

    关注this拼接的正确用法。

    你想要的是这样的:

      todos.splice(yourIndex,1);
    
    

    第一个参数是您要操作数组的起始索引,第二个参数是计数。拼接到位,将修改数组。

    【讨论】:

      【解决方案3】:

      您可以将循环更改为 do while 循环以检查退出,而不是在最后检查时使用 break

      那么你需要存储indexOf的结果,并将item与索引拼接起来。

      let input;
      const todos = [];
      
      do {
        input = prompt('Type what you want to do');
        if (input === 'new') {
          input = prompt("What's your todo?");
          todos.push(input);
        } else if (input === 'list') {
          console.log(`This is your list of todos: ${todos}`);
        } else if (input === 'delete') {
          input = prompt('Which todo would you like to delete?');
          const index = todos.indexOf(input)
          if (index !== -1) {
            todos.splice(index, 1);
            console.log(`You've deleted ${input}`);
          }
        }
      } while (input !== 'exit');

      更好的方法是使用switch statement

      let input;
      const todos = [];
      
      do {
          input = prompt('Type what you want to do');
          switch (input) {
              case 'new':
                  input = prompt("What's your todo?");
                  todos.push(input);
                  break;
              case 'list':
                  console.log(`This is your list of todos: ${todos}`);
                  break;
              case 'delete':
                  input = prompt('Which todo would you like to delete?');
                  const index = todos.indexOf(input)
                  if (index !== -1) {
                      todos.splice(index, 1);
                      console.log(`You've deleted ${input}`);
                  }
          }
      } while (input !== 'exit');

      【讨论】:

      • 我一定会改进的。感谢您抽出宝贵时间提供帮助!
      【解决方案4】:

      使用 indexOf 方法找到索引,然后使用 splice 方法!

      let todos = ['one' , 'two', 'three'];
      let index;
      
      // Wrap the whole code in function to make it resusable
      let user_input = prompt(" Enter your to-do to delete ");
      index = todos.indexOf(user_input);
      
      if (index !== -1) {
        todos = todos.splice(index ,1);
        console.log("Entered to-do deleted successfully");
      } else {
        alert("Entered to-do doesn't exists");
      }
      

      还请记住,作为初学者,有问题是件好事!

      【讨论】:

      • 感谢您在这件事上的提示!已经在查看文档以更好地理解和练习。再次感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 2013-11-28
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      相关资源
      最近更新 更多