繁体   English   中英

从数组中删除特定元素

[英]Removing a specific element from an array

我有一个名为 hotelList 的数组,提示要求用户输入

name numberOfRooms numberOfFloors surfaceOfHotel

现在,例如,当我想删除位于数组中间的酒店时,我正在尝试这段代码:

function eliminateHotel() {
  const userPrompt = prompt(
    "introduce el nombre del hotel que quieras eliminar"
  );
  console.log(hotelList.indexOf(userPrompt === hotelList.name));
  console.log (hotelList.splice(hotelList.indexOf(userPrompt === hotelList.name)))
  console.log(hotelList)

但是当我尝试这样做时,它只会消除数组的最后一个元素。 不是我选的那个。

你能帮忙吗?

非常感谢

尝试使用findIndex

const index = hotelList.findIndex(el => userPrompt  == el.name);
if(index > -1) hotelList.splice(index,1);

暂无
暂无

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

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