简体   繁体   English

如何修改数组对象的值并在 Javascript 上一次比较它?

[英]How can I modify values of array objects comparing it at once on Javascript?

I am printing on my map locations provide me by API.我正在通过 API 打印我的地图位置。 In some cases locations return the same lat and lng , if for example I get 5 locations and 3 locations have the same locations i only see 3 ubications, because 3 locations is in the same point.在某些情况下,位置返回相同的 lat 和 lng ,例如,如果我得到 5 个位置并且 3 个位置具有相同的位置,我只会看到 3 个泛化,因为 3 个位置在同一点。

My quickly solution i think is : loop my object array and add 0.00001 or 0.0002 to look all locations on my map .我认为我的快速解决方案是:循环我的对象数组并添加 0.00001 或 0.0002 以查看我地图上的所有位置。

let points=
[{
        "id": 1,
        "description": "LOC 1",
        "latitude": 37.3676908,
        "longitude": -6.0432685
    },
    {
        "id": 2,
        "description": "LOC 2",
        "latitude": 37.3676908,
        "longitude": -6.0432685
    },
    {
        "id": 3,
        "description": "LOC 3",
        "latitude": 37.3676908,
        "longitude": -6.0432685
    },
    {
        "id": 5,
        "description": "LOC 4",
        "latitude": 37.363128,
        "longitude": -6.0455416
    },
    {
        "id": 6,
        "description": "LOC 5",
        "latitude": 37.369319,
        "longitude": -6.0421527
    }
]

I want to get the next :我想得到下一个:

let points=
[{
        "id": 1,
        "description": "LOC 1",
        "latitude": 37.3676908,
        "longitude": -6.0432685
    },
    {
        "id": 2,
        "description": "LOC 2",
        "latitude": 37.3676909,
        "longitude": -6.0432686
    },
    {
        "id": 3,
        "description": "LOC 3",
        "latitude": 37.3676910,
        "longitude": -6.0432687
    },
    {
        "id": 5,
        "description": "LOC 4",
        "latitude": 37.363128,
        "longitude": -6.0455416
    },
    {
        "id": 6,
        "description": "LOC 5",
        "latitude": 37.369319,
        "longitude": -6.0421527
    }
]

I've tried to achieve that with .sort and .filter but I dont find nice solution to it.我试图用 .sort 和 .filter 来实现这一点,但我没有找到很好的解决方案。

Could someone help me .有人可以帮我吗。 Thank you in advance先感谢您

I think you want map if you want to do an operation to every object in an array.如果您想对数组中的每个对象进行操作,我认为您需要map

points.map(x => x + Math.random()/1000) would add a random tiny number. points.map(x => x + Math.random()/1000)会添加一个随机的points.map(x => x + Math.random()/1000)

But you can do whatever function you want on x after the x => part.但是您可以在x =>部分之后对x =>执行任何您想要的功能。 Maybe something like也许像

points.map(myfunc) and you can define myfunc as add .0001 or add .0002 based on some condition. points.map(myfunc)并且您可以根据某些条件将 myfunc 定义为 add .0001 或 add .0002 。

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

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