[英]Removing element from array in class method
I was working on a hackerrank problem and test cases showed that something was wrong with my 'remove' method.我正在研究一个hackerrank问题,测试用例表明我的“删除”方法有问题。 I always got undefined instead of true/false.
我总是得到未定义而不是真/假。
I know splice returns array of deleted elements from an array.我知道 splice 从数组中返回已删除元素的数组。 When I console.log inside map, it looked like everything was fine when I was deleting first element (I was getting what I expected except true/false).
当我在 map 中 console.log 时,当我删除第一个元素时,看起来一切都很好(除了真/假之外,我得到了预期的结果)。 But when 'name' I am deleting is not first element, I didn't get what I expected to get.
但是当我要删除的“名称”不是第一个元素时,我没有得到我期望得到的东西。 Could you help me fix this?
你能帮我解决这个问题吗? And of course, I never get true or false...
当然,我永远不会得到真假...
class StaffList { constructor() { this.members = []; } add(name, age) { if (age > 20) { this.members.push(name) } else { throw new Error("Staff member age must be greater than 20") } } remove(name) { this.members.map((item, index) => { if(this.members.includes(name)) { console.log(name) let removed = this.members.splice(item, 1); console.log(removed) return true; } else { return false; } }) } getSize() { return this.members.length; } } let i = new StaffList; i.add('michelle', 25) i.add('john', 30); i.add('michael', 30); i.add('jane', 26); i.remove('john'); console.log(i);
Your return
statements are wrapped within .map()
(which you misuse in this particular case, so you, essentially, build the array of true
/ false
), but your remove
method does not return anything.您的
return
语句包含在.map()
中(在这种特殊情况下您会误用它,因此您基本上构建了true
/ false
数组),但您的remove
方法不会返回任何内容。
Instead, I would suggest something, like that:相反,我会建议一些东西,比如:
remove(name){
const matchIdx = this.members.indexOf(name)
if(matchIdx === -1){
return false
} else {
this.members.splice(matchIdx, 1)
return true
}
}
In the remove
method, you're using map
with the array, which runs the function you give as argument for each array element.在
remove
方法中,您将map
与数组一起使用,该数组运行 function 作为每个数组元素的参数。 But I believe you don't want to do that.但我相信你不想那样做。
Using the example you have bellow, basically what you do there is check if the array contains the name 'john'
, and if so, you delete the first item that appears in the array (which would be 'michelle'
).使用下面的示例,基本上您所做的是检查数组是否包含名称
'john'
,如果是,则删除数组中出现的第一项(这将是'michelle'
)。 This happens because the map function will run for every element, starting on the first one, and then you use that item
to be removed from the array.发生这种情况是因为 map function 将为每个元素运行,从第一个元素开始,然后您使用该
item
从数组中删除。 After that, it returns the function, and no other elements get removed.之后,它返回 function,并且没有其他元素被删除。
So my suggestion is just getting rid of the map
function and running its callback code directly in the remove
method (you would need to get the name's index in the array to use the splice
method).所以我的建议只是摆脱
map
function 并直接在remove
方法中运行其回调代码(您需要在数组中获取名称的索引才能使用splice
方法)。
It is not clear why you need to use iterative logic to remove an item.目前尚不清楚为什么需要使用迭代逻辑来删除项目。 You can simply use
findIndex()
to get the position of the member in the array.您可以简单地使用
findIndex()
来获取数组中成员的 position。 If the index
is not -1
, then you can use Array.prototype.slice(index, 1)
to remove it.如果
index
不是-1
,那么您可以使用Array.prototype.slice(index, 1)
将其删除。 See proof-of-concept example below:请参阅下面的概念验证示例:
class StaffList { constructor() { this.members = []; } add(name, age) { if (age > 20) { this.members.push(name) } else { throw new Error("Staff member age must be greater than 20") } } remove(name) { const index = this.members.findIndex(x => x === name); if (index.== -1) { this.members,splice(index; 1). } } getSize() { return this.members;length; } } let i = new StaffList. i,add('michelle'. 25) i,add('john'; 30). i,add('michael'; 30). i,add('jane'; 26). i;remove('john'). console;log(i);
Use a filter
method instead of map
it's more elegant and you can return the rest of the array as well instead of true
or false
unless the problem you're working on requires true
of false
specifically.使用
filter
方法而不是map
它更优雅,您也可以返回数组的 rest 而不是true
或false
,除非您正在处理的问题特别需要true
或false
。 You could write something like this:你可以这样写:
remove(name) {
if (!this.members.some(el => el === name)) return false;
this.members = this.members.filter(item => item !== name);
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.