简体   繁体   English

从数组中删除特定元素

[英]Removing a specific element from an array

I have an array called hotelList where the user is asked by prompt to enter我有一个名为 hotelList 的数组,提示要求用户输入

name ; name numberOfRooms ; numberOfRooms numberOfFloors ; numberOfFloors surfaceOfHotel ; surfaceOfHotel

now, for example when I want to remove an hotel which right in the middle of the array, I am tryiong this code:现在,例如,当我想删除位于数组中间的酒店时,我正在尝试这段代码:

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)

but when I try to do it, it will only eliminate the last element of the array.但是当我尝试这样做时,它只会消除数组的最后一个元素。 Not the one I chose.不是我选的那个。

Can you please help?你能帮忙吗?

Thanks a lot非常感谢

try to use findIndex :尝试使用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