Javascript数组对象需要删除元素

标签 javascript arrays google-maps google-maps-api-3 javascript-objects

Google map directionsService.route 需要使用 javascript 添加删除 waypoint

可以使用代码向数组添加新点:

waypts.push({ location: "Rome, Italy", stopover: true });

从数组中删除航路点时出现问题

Here my some data example that can explain more brief;

Need to remove point 1 or point 2 from above javascript object array how do it possible.

Then I will create new waypts for each time route draw from above array named nwaypts.

var nwaypts = [
{ location: "Rome, Italy", stopover: true }, // xyz: start point 
{ location:  { lat: 42.2070007, lng: 12.48549939999998 }, stopover: true, id: "17167" }, // point 1
{ location: { lat: 42.3793831, lng: 12.82857039999999 }, stopover: true, id: "18823" }, // point 2
{ location: "Terni, Italy", stopover: true } // xyz: end point 
];

最佳答案

您好,在删除航点中,您可以将 id 添加到标记,然后在 removecode 期间,您可以尝试与标记 id(您想要删除的)进行比较。请提供完整代码以获得确切的解决方案。

var nwaypts=[];
                        var j=0;
                        for ( var i = 0; i < waypts.length ; i++) {
                            if(waypts[i].id!=removemarkerid){
                                nwaypts[j]=waypts[i];
                                j++;
                            }
                        }
                        waypts=nwaypts; 

关于Javascript数组对象需要删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40466934/

相关文章:

javascript - 使用扩展运算符更新对象值

javascript - 在 Ajax 成功时强制下载 .mp3 或 .zip 文件

javascript - 如何循环遍历 React 组件中数组内对象的属性?

javascript - 发布到GEOJSON Google 将数据映射到我的远程服务器上的 json 文件

javascript - 子 div 元素未出现在由 JavaScript 函数创建的父 div 元素中

javascript - Bootstrap 轮播在点击时向上移动

c - 当 actual 和 extern 类型不同时会发生什么?

java - 更新 ArrayList

Android map api-2 禁用点击

javascript - 查找最近的已知位置 : Google Reverse Geocoding