I realize hashes have unique keys already, but my question is aimed at a particular use case (that I feel may be common) regarding hashes.
I have a google map (v3) on my website. I load a bunch of locations from my database, and throw markers on the map at each location. I had an issue in my application where I was seeing duplicate markers for every location. I decided to add a check at the top of my add marker function, to use a hash of previously placed markers (stored by location).
markers = {};
function addMarker(latlng) {
if (!markers.hasOwnProperty(latlng)) {
markers[latlng] = true;
} else {
return;
}
// do other stuff
}
This works, but it feels hacky. Adding the unused boolean value to each hash key/pair just doesn't feel right, because it's technically not used... I just need the key half.
Is there another approach to accomplish the same idea?
Related
Set and Map both are newer data types in es6 and for certain situations both can be used.
e.g - if i want to store all unique elements, i can use Set as well as Map with true as value.
const data: string[] ;
// console.log('data', data[0])
const set = new Set();
const map = new Map<string, boolean>();
data.forEach((item) => {
map.set(item, true);
});
data.forEach((item) => {
set.add(item);
});
Both works, but i was wondering which one is faster ?
Update 1
I am looking for which of the data structure is faster in case of storing data.
checking if value exist using -
map.has(<value>)
set.has(<value>)
deleting values
Also i can understand true is redundant and not used anywhere, but i am just trying to show how map and set can be used alternatively.
What matters is speed.
In the most basic sense:
Maps are for holding key-value pairs
Sets are for holding values
The true in your map is completely redundant ... if a key exists, that automatically implies, that it is true/exists - so you will never ever need to use the value of the key-value pair in the map (so why use the map at all, if you're never gonna make use of what it is actually for? - to me that sounds like a set/array with extra steps)
If you just want to store values use an array or set. - Which of the two depends on what you are trying to do.
The question of "which is faster" can't really be answered properly, because it largely depends on what you are trying to do with the stored values. (What you are trying to do also determines what data structure to use)
So choose whatever data structure you think fits your needs best, and when you run into a problem that another would fix, you can always change it later/convert from one into another.
And the more you use them and the more you see what they can and can not do, the better you'll get at determining which to use from the start (for a given problem)
As a newbie to ArcGIS and Javascript as a whole, I might be going about this problem the wrong way, especially seeing the samples given. However, I will still try to see if my ideal method is eligible.
In my ArcGIS file, I would like to individually isolate the entries of a FeatureLayer and modify them, mid-runtime. In this case, I would want to hide certain entries, based on their content.
function changeVisibility ()
{
var checkBox = document.getElementById("expLayer");
for (var i = 0; i < featureLayer.length; i++)
{
featureLayer[i].visible = checkBox.checked;
}
}
The above code is incorrect as featureLayer, which is my "FeatureLayer" type, is not an array. I'm currently searching for a method to gather the individual values of featureLayer.
The sample programs that the ESRI website gives has all the separate entries isolated into different layer classes (lake entries are separated from city entries), and then choosing to make the whole layer visible or invisible. Its making me think that isolating entries of a layer isn't possible. Even if it is possible, I'm unsure if each layer entry would have their own "visible" flag.
Is it possible to isolate the individual values of a layer class, and if so, does it take the form of an array?
I just had an interview test in which I had to build a Route suggestion feature using ReactJS. Long story short, I tried to use the Dijkstra's algorithm to find the shortest path between 2 train stations in a network.
In my implementation, I maintained a tracingMap which is a Map object that stores a station X as key. The value of this Map is another station Y which is a neighbor of X that is on the shortest path from the origin station to X. My goal is to recursive call tracingMap.get(destStation) while destStation != origStation in the end in order to rebuild the shortest path from the origin station to destination station.
The skeleton is as following:
// Store all the StationNode using stationId as key
let stationGraph = new Map();
function findShortestPath(origStationId, destStationId) {
let tracingMap = new Map();
... perform the Dijkstra's algorithm ...
return buildRoute(tracingMap, origStationId, destStationId);
}
function buildRoute(tracingMap, origStationId, destStationId) {
...
}
Here is my problem. I have a StationNode class which is my own custom object to implement the graph of stations. Initially, I tried to use this class as both key and value of my tracingMap. I tried to use console.log(tracingMap) at the end of the findShortestPath() function to make sure the map was updated properly and I could see all the entries I added just fine. I even tried to call console.log(tracingMap.get(destStationNode)) to be 10000% sure that I can get the value I need.
However, when the tracingMap arrives at the buildRoute() function, tracingMap.get(stationGraph.get(destStationId)) returns undefined. I couldn't get any values out of it even though console.log(tracingMap) is still showing that ALL entries are still there.
In the end, I had to change my implementation to stores stationId as key and value in the tracingMap. Then it would work just fine when I pass this Map to the other function.
As I'm relatively inexperienced in JavaScript, I'd be very grateful if you could tell me what I've done wrong. I believe I have a wrong understanding somewhere :).
I have an array "markerGroups" defined like this:
var markerGroups = {"foo": [], "bar": [], "foobar": []};
This array is used for storing all the markers in a Google Map. As you can see, I have three categories where I store the markers. One marker will only exist in one category, and will never exist in any other category.
Each marker is also identyfied by a unique ID, for instance the ID can be 1422.
Now I want to add the markers to this group in such a way that i can reach it by doing for instance
console.log(markerGroups.foo[1422]);
Even if that's the only marker in that category. I must also be able to remove it completly from the category.
I have tried to make this possible by defining markerGroups like this:
var markerGroups = {"foo": {}, "bar": {}, "foobar": {}};
This line is in a function where the marker is defined as marker, and category is passed into the function, telling the function where to store the marker.
markerGroups[category][marker.ID] = marker;
This works excellent, except the fact that markers now won't show up in my map.
Any ideas why or how this could be done in a better way?
Markers are simple JS objects that can be manipulated in the same way that any JS object can.
The only way you can control if a Marker is shown on the map is via it's setMap() function. So check out your code and see if you call setMap(your_map) for the markers you want to be shown on the map. Also bear in mind that there are some properties that must be set (position for example) in order the Marker to be shown.
So, don't worry of simple JS object manipulation, it doesn't interact with the Marker visibility, as long as it's initialized properly.
Of course, this is a theoretical point of view, if you've done all this and it still doesn't work, you must provide some additional details and code.
I'm attempting to create a dynamic map using the latest Google Map API. Everything is going smooth so far. I do have a question of course:
How would I/you go about saving my current map?
Let's say you build a map with dynamic markers: Since everything is done via JavaScript, I need to get/set those values from a file/database.
I was thinking about outputting the entire google.map.Markers as a JSON and send it to a database as a string, but then if I have around 100 places, I'm not sure how well it would go and I'm worried about efficiency.
Is this the only way to do it and am I thinking properly? Basically your website users are allowed to place a marker on the map, which then are subject to confirmation of course. Once it's confirmed, that marker must be in the latest "version" of the map, so I must get that info from/into a database/file.
Thanks in advance!
I'm creating similar application and I implemented it this way:
I have a Marker table which has columns for each attribute that I'm using in markers (e.g. latitude, longitude, name, description, type, etc.) When someone adds a marker I'm just saving the attributes of the marker to the database. Next time I want to show the marker I'm just getting the attributes from database, encode them to JSON and attach to the page. Inside page I have the JS that grabs those attributes and creates the markers inside the map. Pseudocode:
//this is generated dynamically from DB.
var markers = [
{lat:115416,lng:26411},
{lat:115416,lng:26411}
];
//this is static, just grabs the dynamic bit and puts it on the map:
for (var i = 0; i < markers.length; i++){
//creates a marker object
var marker = new Marker({lat:markers[i].lat, lng:markers[i].lng })
//displays it on the map
map.addMarker(marker);
}
the benefit of this is that your data in the database is independent from map implementation, e.g. if in the future you decide to move to apple maps and it has different implementation you can just write different JS to handle the data. Also you can query over it, e.g. you can query for places that are close by looking at lat and lang, etc..