I have an array, this array contains objects that have properties, for example: {Name, lat, lng}.
我有一个数组,这个数组包含具有属性的对象,例如:{Name,lat,lng}。
I have to do that if two objects have the same value both of lat and of lng, then I have to modify both the values of lat and lng only in the second object, in a few words I have to add a very small numerical value so that they are different.
我必须这样做,如果两个对象具有lat和lng的相同值,那么我必须仅在第二个对象中修改lat和lng的值,换句话说我必须添加一个非常小的数值这样它们就不同了。
Example of an array:
数组示例:
var array = [
{
"id": "269849444",
"name": "Ziano Piacentino",
"lat": 45,
"lng": 9.4,
},
{
"id": "649296407",
"name": "Monte Bondone",
"lat": 46.0315,
"lng": 11.05685,
},
{
"id": "300151628",
"name": "Cima Calisio",
"lat": 46.0977158258,
"lng": 11.1443512052,
},
{
"id": "266239592",
"name": "Trient, Trentino-Alto Adige, Italy",
"lat": 46.0667,
"lng": 11.1333,
},
{
"id": "130313194355778",
"name": "Monte Celva",
"lat": 46.0695882,
"lng": 11.1783065,
},
{
"id": "217942785",
"name": "Trento, Italy",
"lat": 46.0667,
"lng": 11.1333,
},
{
"id": "478657266",
"name": "Mercatino di Natale a Levico Terme città da amare",
"lat": 46.0099217576,
"lng": 11.3052625593,
},
{
"id": "288554028181059",
"name": "Fontana Del Nettuno, Trento",
"lat": 46.0675553413,
"lng": 11.1213752236,
},
{
"id": "252747884",
"name": "Duomo di Milano - Milan Cathedral",
"lat": 45.4646680426,
"lng": 9.1904055604,
},
{
"id": "213183830",
"name": "Piacenza",
"lat": 45.0167,
"lng": 9.66667,
},
{
"id": "213183830",
"name": "Home",
"lat": 45.0167,
"lng": 9.66667,
}
];
Any suggestions? Even if only solution I think is to use a double for.
有什么建议?即使我认为只有解决方案是使用双倍。
2 个解决方案
#1
1
Try this:
var array = [...];
array.forEach(function(obj1, key1){
var increment = 0.000001;
array.forEach(function(obj2, key2){
if (key1 != key2){
if(obj1.lat == obj2.lat && obj1.lng == obj2.lng){
array[key2].lat += increment;
array[key2].lng += increment;
increment += 0.000001;
}
}
});
});
#2
1
The steps to solving a problem with code are:
解决代码问题的步骤是:
- Understand the problem
- Understand what you need to to do to solve it
- Plan it out in pseudocode
- Code
了解问题
了解解决问题需要做什么
用伪代码计划出来
Congratulations, the first two steps are completed. I can help out with the third and you'll be ready for the fourth!
恭喜,前两步已经完成。我可以帮助第三个,你将为第四个做好准备!
The most basic approach would be to have two loops, where you search each item in the array until you find one that matches the criteria and carry out what you need to do.
最基本的方法是有两个循环,在这里您搜索数组中的每个项目,直到找到符合条件的项目并执行您需要执行的操作。
The following is not actual code syntax, it's pseudocode. A way for you to understand more in depth what road to take in order to solve your problem.
以下不是实际的代码语法,它是伪代码。一种让您更深入地了解解决问题的道路的方法。
var array = {...}
for elementA in array {
for elementB in array{
//First you have to make sure you are not comparing the same value in the array with itself
if elementA.index is different from elementB.index then {
if elementA equals elementB {
change elementB
}
}
}
}
So what you do in this pseudocode is take an element from your array, and match it against all the other items in the same array. You have to be careful not to try and match against itself, one way to avoid this is by comparing the item's index position, if they are different, you can be pretty sure that it's not same item. Once you passed that hurdle, all you have to do then is ask if they are the same (in your case that would specifically be if they have the same values for lat
and lng
). If they are, then all you have to do is change elementB
, which is the second element compared.
因此,您在此伪代码中执行的操作是从数组中获取一个元素,并将其与同一数组中的所有其他项匹配。你必须小心不要试图与自己匹配,避免这种情况的一种方法是通过比较项目的索引位置,如果它们不同,你可以非常确定它不是同一个项目。一旦你通过了这个障碍,你所要做的就是询问它们是否相同(在你的情况下,特别是如果它们具有相同的lat和lng值)。如果是,那么你所要做的就是改变elementB,这是比较的第二个元素。
That's it!
Hope that helps set you on the right path. You can get more out of * if you offer a bit of how you've approached the problem, instead of asking for a raw answer.
希望这有助于您走上正确的道路。如果你提供一些你接近问题的方法,而不是要求原始答案,你可以从*中获得更多。
#1
1
Try this:
var array = [...];
array.forEach(function(obj1, key1){
var increment = 0.000001;
array.forEach(function(obj2, key2){
if (key1 != key2){
if(obj1.lat == obj2.lat && obj1.lng == obj2.lng){
array[key2].lat += increment;
array[key2].lng += increment;
increment += 0.000001;
}
}
});
});
#2
1
The steps to solving a problem with code are:
解决代码问题的步骤是:
- Understand the problem
- Understand what you need to to do to solve it
- Plan it out in pseudocode
- Code
了解问题
了解解决问题需要做什么
用伪代码计划出来
Congratulations, the first two steps are completed. I can help out with the third and you'll be ready for the fourth!
恭喜,前两步已经完成。我可以帮助第三个,你将为第四个做好准备!
The most basic approach would be to have two loops, where you search each item in the array until you find one that matches the criteria and carry out what you need to do.
最基本的方法是有两个循环,在这里您搜索数组中的每个项目,直到找到符合条件的项目并执行您需要执行的操作。
The following is not actual code syntax, it's pseudocode. A way for you to understand more in depth what road to take in order to solve your problem.
以下不是实际的代码语法,它是伪代码。一种让您更深入地了解解决问题的道路的方法。
var array = {...}
for elementA in array {
for elementB in array{
//First you have to make sure you are not comparing the same value in the array with itself
if elementA.index is different from elementB.index then {
if elementA equals elementB {
change elementB
}
}
}
}
So what you do in this pseudocode is take an element from your array, and match it against all the other items in the same array. You have to be careful not to try and match against itself, one way to avoid this is by comparing the item's index position, if they are different, you can be pretty sure that it's not same item. Once you passed that hurdle, all you have to do then is ask if they are the same (in your case that would specifically be if they have the same values for lat
and lng
). If they are, then all you have to do is change elementB
, which is the second element compared.
因此,您在此伪代码中执行的操作是从数组中获取一个元素,并将其与同一数组中的所有其他项匹配。你必须小心不要试图与自己匹配,避免这种情况的一种方法是通过比较项目的索引位置,如果它们不同,你可以非常确定它不是同一个项目。一旦你通过了这个障碍,你所要做的就是询问它们是否相同(在你的情况下,特别是如果它们具有相同的lat和lng值)。如果是,那么你所要做的就是改变elementB,这是比较的第二个元素。
That's it!
Hope that helps set you on the right path. You can get more out of * if you offer a bit of how you've approached the problem, instead of asking for a raw answer.
希望这有助于您走上正确的道路。如果你提供一些你接近问题的方法,而不是要求原始答案,你可以从*中获得更多。