0

I'm working with Google Maps API v3 and I have an array of markers (markersArray) that each contain an element named 'id'. Via Ajax, I hit a db and return current ID values as an array (tokID) and call a 'loadMarkers' function.

In the 'loadMarkers' function I need to remove any array items that don't match the ID values returned from the db. I've looked into .splice() and .filter() but can't for the life of me get it working.

Sorry for no code sample. I'm really stumped and don't really have anything to go off of.

Thx! Mike

MikeyJ
  • 37
  • 4
  • 2
    "Sorry for no code sample. I'm really stumped and don't really have anything to go off of." Maybe you should come back when you have something for us to work with. – j08691 Jun 14 '12 at 17:09
  • Take a look here: http://stackoverflow.com/questions/4825812/clean-way-to-remove-element-from-javascript-array-with-jquery-coffeescript – Josh Siok Jun 14 '12 at 17:13

2 Answers2

0

I am not sure what data you have and what format you get data before calling loadMarkers but if you are using JQuery and you want to remove items from an array based on a list of ids from another array here is how you do that:

​var orgArray = [{id:1},{id:2},{id:3}];
var newArray = [1,3];

var orgArray = $.map(orgArray, function(v, i){ return $.inArray(v.id, newArray) ? v : null;})​;

console.log(orgArray);
Wiz
  • 477
  • 5
  • 17
  • Thanks for the reply! Using your example, can you tell me what results I would expect to see if the newArray matches the orgArray when the code is run? I think I see that it's removing id:1? In my particular case, I wouldn't want anything to happen to the original array if the new id values are the same as what's already in the original. Thx! – MikeyJ Jun 14 '12 at 19:15
  • are you saying even the newArray would be an array with id property like `[{id:1},{id:3}]`? If you dont want to change the original array what do you want to do? I am sorry but I am not getting your question here – Wiz Jun 14 '12 at 19:34
  • Sorry for the confusion. I definitely want the original array `[{id:1},{id:2},{id:3}]` to change if the new array is something like `[1,3]`. If the new array is the same as original, `[1,2,3]`, I don't want any change but it seems like when they are the same, your code is causing `{id:1}` to be removed from the array? I'm sorry if i'm interpreting the results incorrectly...this is all new to me. :) – MikeyJ Jun 14 '12 at 20:34
  • Ok. As a test, when I run the code you posted, in Firebug Console I get back `[Object { id=2}, Object { id=3}]`. Is that the expected output? – MikeyJ Jun 14 '12 at 20:43
  • Yes that is the expected result but I am not sure what is your requirement. Do you want to only remove the ids which do not exist in the new array? – Wiz Jun 15 '12 at 06:29
  • Yes, that's exactly what I need. Joy's code above seems to accomplish what I needed although I'm still trying to figure out where I should run my `addMarkers` function if the new array contains an ID that is NOT in the original array. :) – MikeyJ Jun 15 '12 at 06:39
0

Roughly you can do something like this,

UPDATED WORKING CODE

var newIDs; // new IDs found from ajax

newIDs = [1,2]
markersArray = [{ID:1},{ID:8},{ID:2},{ID:4},{ID:6}]
var newMarkers = $.map(markersArray,function(obj,i){
   if(($.inArray(obj.ID,newIDs) == -1))
      return null;
   else
      return obj; 
});

console.log(newMarkers);
​

// now newMarkers holds the filtered markers Array

Modify the above code to suit your need.

$.map $.inArray

Check the Working Fiddle

Final Code to find out the Extra ID's sent from server too is : Fiddle

var newIDs; // new IDs found from ajax

newIDs = [1,2,3,4,5];

markersArray = [{ID:1},{ID:8},{ID:2},{ID:4},{ID:6}];
console.log('Orig Array: ',markersArray);
console.log('From Server: ',newIDs);
var newMarkers = $.map(markersArray,function(obj,i){
    return $.inArray(obj.ID,newIDs) == -1? null: obj;
});

console.log('New Markers Objects: ',newMarkers);
var newMarkerIDs = $.map(markersArray,function(obj,i){
    return $.inArray(obj.ID,newIDs) == -1? null:obj.ID;
});
console.log('New Markers ID Array: ',newMarkerIDs);
var extraIDs = $.map(newIDs,function(val,i){
    return $.inArray(val,newMarkerIDs) == -1 ? val : null;
});

console.log('Extra IDs: ',extraIDs);
​
Prasenjit Kumar Nag
  • 13,391
  • 3
  • 45
  • 57
  • Hi Joy! Thanks for the reply. Trying your code with the test values `[{id:1},{id:2},{id:3}]` and `[1,3]` returns `[Object { ID=1}, Object { ID=2}, Object { ID=3}]` in Firebug console. Is this correct or should it only return `[Object { ID=1}, Object { ID=3}]`? Thx! – MikeyJ Jun 15 '12 at 05:18
  • Thx Joy! This seems to be working! If the newIDs returned an ID that was NOT in the markersArray, how would I handle that? I have an addMarkers function, I'm just not sure where to execute it... – MikeyJ Jun 15 '12 at 06:13
  • Or even better this one http://jsfiddle.net/joycse06/Q6VCv/3/ to check all values sequentially in console. – Prasenjit Kumar Nag Jun 15 '12 at 06:54
  • Just wrote a `dummyAddmarker` func to demonstrate how you can loop through new ids and add each as a marker. Have a look here http://jsfiddle.net/joycse06/Q6VCv/5/ – Prasenjit Kumar Nag Jun 15 '12 at 07:14
  • Hey Joy, things are working great! One more question. Using your latest fiddle, how would I create an array containing only the "unused" IDs from the orig Array? That would be ID's 6 and 8 using your example. Thx! I tried fooling with the `inArray` method but couldn't get the right results. – MikeyJ Jun 18 '12 at 17:43
  • Check the last portion of code in this fiddle http://jsfiddle.net/joycse06/Q6VCv/6/ You just need to swap `null` and `obj` which will invert the logic. – Prasenjit Kumar Nag Jun 18 '12 at 17:48