从 JavaScript 数组中删除任意元素

发布于 2024-07-15 19:58:22 字数 1294 浏览 6 评论 0原文

在我的 Google 地图应用程序中,我可以在地图上放置标记,并保留对每个放置的标记的引用,以及名为 markers 的数组中的一些额外信息。

添加标记很容易,我只需将新创建的对象push()到数组上(markers.push(marker));

然而,当需要从数组中删除任意标记时,给定槽的索引,它的行为不会按预期进行。 我的功能是:

function deleteMarker(markerIndex) {
    if (markerIndex!='' && markerIndex>=0 && markerIndex<markers.length) {
        if (confirm('Do you really want to remove this marker from the map?')) {
            alert('deleting marker '+markerIndex); //debugging purposes
            markers.splice (markerIndex, 1);
        }
    }
}

我以前没有使用 splice() 函数的经验,但是查看 其描述 @ w3schools 看起来非常简单。 但是,我得到以下行为:

markers.splice() 不执行任何操作。 那么我做错了什么?

而且,当 markerIndex 为 0 时,不会显示确认框。 起初,我假设冗长的 if 条件评估为 false,因此跳过了整个代码块,但是,使用 Firebug 逐步执行调用,我发现当数组非空时,条件(当然)适用于索引 0 ,下一步显示 if (confirm(...))alert('deleting...)跳过并且>markers.splice() 被调用(但没有任何反应)。 这种行为很奇怪,我决定提出这个问题。

谁能澄清一下发生了什么吗?

我认为删除标记将是人们可以做的最简单的功能。 我可以添加它们,编辑它们的内容,甚至清除所有标记pop()-将标记从markers数组中删除,直到为空)以及所有效果很好。

In my Google Maps application I can place markers on the map, and I keep a reference to each of the markers placed, along with some extra information in an array called markers.

Adding markers is easy, I just push() the newly created object onto the array (markers.push(marker));

However, when it comes to removing an arbitrary marker from the array, given an index of the slot, it doesn't behave as expected. My function is:

function deleteMarker(markerIndex) {
    if (markerIndex!='' && markerIndex>=0 && markerIndex<markers.length) {
        if (confirm('Do you really want to remove this marker from the map?')) {
            alert('deleting marker '+markerIndex); //debugging purposes
            markers.splice (markerIndex, 1);
        }
    }
}

I have no previous experience with the splice() function, but looking at its description @ w3schools it seems to be pretty straight-forward. However, I get the following behaviour:

markers.splice() does nothing. So what am I doing wrong?

And also, when markerIndex is 0 no confirmation box is shown. At first I assumed the lengthy if-condition evaluated to false and so the whole code block was skipped, however, using Firebug to step through the calls I found out that the condition holds (of course) for index 0 when array is non-empty, next step reveals that the if (confirm(...)) and alert('deleting...) are skipped and markers.splice() is called (but nothing happens). This behaviour is so strange I decided to open this question.

Can anyone please clarify what's going on?

I thought that deleting markers will be the easiest bit of functionality one could do. I can add them, edit their contents, even clear all markers (pop()-ing markers off the markers array until empty) and all works nicely.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任性一次 2024-07-22 19:58:22

您的代码存在一个问题,即 JavaScript 将 0 == '' 解释为 true,因此对于为零的 markerIndex,您的确认代码不会执行。 我猜您误解了 Firebug 显示的步骤,或者它只是这里有问题,因为您的 if 条件实际上对于标记索引 0 计算为 false。

您可以通过添加一个来使用类型严格比较 extra =

if (markerIndex !== '' && ...) {

更简单的方法是:

if (markers[markerIndex] !== undefined) {

因为 JavaScript 在访问未定义的对象成员时不会引发错误。

您的另一个问题 splice() 无法正常工作很奇怪(它应该可以工作)。

One problem with your code is that JavaScript interprets 0 == '' as true, so for a markerIndex of zero, your confirm-code is not executed. I guess that you misinterpreted the steps Firebug shows or that it simply is buggy here since your if-condition will in fact evaluate to false for a markerIndex of 0.

You can use type-strict comparison by adding an extra =:

if (markerIndex !== '' && ...) {

An easier approach would be:

if (markers[markerIndex] !== undefined) {

Since JavaScript does not raise an error when accessing undefined object members.

Your other problem with splice() not working is weird (it should work).

圈圈圆圆圈圈 2024-07-22 19:58:22
function deleteMarker(markerIndex) {
    if (markers[markerIndex] !== undefined) {
        if (confirm('Do you really want to remove this marker from the map?')) {
            map.removeOverlay(markers[markerIndex]);
            markers.splice (markerIndex, 1);
        }
    }
}

这似乎可以解决所有问题。 感谢您的回答,非常感谢。
我特别喜欢将第一个 if 条件简化为:

if (markers[markerIndex] !== undefined)

splice() 似乎直接在数组上操作(与 NuclearDog 暗示的相反)并且它按预期工作。 我最亲爱的萤火虫误导了我。

function deleteMarker(markerIndex) {
    if (markers[markerIndex] !== undefined) {
        if (confirm('Do you really want to remove this marker from the map?')) {
            map.removeOverlay(markers[markerIndex]);
            markers.splice (markerIndex, 1);
        }
    }
}

This seems to take care of all the problems. Thanks for your answers, much appreciated.
I especially like the idea of reducing the first if-condition to just:

if (markers[markerIndex] !== undefined)

splice() seems to operate directly on the array (contrary to what NuclearDog hinted) and it works as expected. My dearest Firebug mislead me.

谈下烟灰 2024-07-22 19:58:22

不确定这是否是这里发生的事情。 但我看到相同的代码在 Firebug 中在调试时和未调试时运行不同。 我很确定我也没有干扰代码的手表。

not sure if this is what is happening here. but i've seen the same code run differently in firebug when it is being debugged v when it is not being debugged. i'm pretty sure i had no watches that were interfering with the code either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文