1

i'd like to "define" 2 variables as 1 new variable, which then contains the contents/data of both previous variables. Let's say the first variable is called 'var A' and the second 'var B', can i combine those 2 in a new variable simply like this?

var ALL = var A + var B;

..or how is the correct syntax for this? I hope this isn't too abstract? ;)

var A and B are both variables defining external geojson files, and I'd like to be able to "combine" these 2 in 1 new variable.

julia
  • 301
  • 1
  • 4
  • 9

3 Answers3

1

I would recommend using a function to handle combining them.

function combine(A,B) {
    var C = {};
    C.stuff_from_A = A.some_info;
    C.stuff_from_B = B.some_info;
    return C;
}

now you can perform what you ask.

var C = combine(A,B);

EDIT:

An example involving location data:

function combine(A,B) {
    var C = {};
    C.position_of_A = A.coordinate_info;
    C.position_of_B = B.coordinate_info;
    return C;
}

or to store the midpoint between them:

function combine(A,B) {
    var C = {};
    C.midpoint = average(A.location,B.location);
    return C;
}

or more generally:

function combine() {
    var C = {}; // Initialize

    // Logic to combine and store info

    return C; // Return the new object
}

EDIT 2

C.totalMarkers = [];
for (var i = 0; i < numberOfMarkersInA; i++) {
    C.push(A.getMarker(i));
}
for (var i = 0; i < numberOfMarkersInB; i++) {
    C.push(B.getMarker(i));
}

That is pseudo-code, those variable names will need to changed of course.

Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
  • Thanks for the pointer! Sounds great to me - but I'm new to all this, so please bear with me ;) What do I have to replace `stuff_from_A` and `some_info` with? – julia Apr 15 '14 at 19:23
  • var A and B both link to external geojson .js files which contain map coordinates etc. The variables A and B are linked like this: `var A = L.geoJson(data);` – julia Apr 15 '14 at 19:30
  • @julia it depends on how you would like the data to be combined. This strategy allows you to combine the two `geoJson` objects exactly how you would like. See my edit for an example. – Farmer Joe Apr 15 '14 at 19:35
  • @julia hope those examples help, but beyond that you would need to give me more information about what you want the resultant object to store, and explain how it is related to `A` and `B` – Farmer Joe Apr 15 '14 at 19:41
  • thanks again! Unfortunately I'm still not sure how to achieve this.. Some more background info: A and B each link to a different geojson file with multiple (50+) map markers in it (with coordinates, names, descriptions and lots of other infos). I had implemented a Leaflet search plugin in order to search for markers on my map - BUT in the search plugin I can only define 1 variable as the search layer - that's why I was trying to merge the infos from A+B in 1 variable in order to circumvent this restriction… does that help in any way? – julia Apr 15 '14 at 19:46
  • @julia Are you saying you would like an object that stores the map markers of both `A` and `B`? if so you need to implement it exactly as you say in your `combine` function, see my new edit – Farmer Joe Apr 15 '14 at 19:48
  • Yes, that's exactly what I'm trying to achieve (an object storing the map markers of both A+B). Many thanks for the example, I'm still trying to implement it though.. – julia Apr 16 '14 at 11:46
  • @julia no problem, If you want any further help I would need to see maybe some pseudo mock up of what your objects look like, and what you want the combined object to look like. – Farmer Joe Apr 16 '14 at 14:02
0

If there are the objects - serialize they and add with separator. If there is simple string, number, bool or something else, add with separator directly.

Albert221
  • 5,223
  • 5
  • 25
  • 41
  • var A and B both link to external geojson .js files which contain map coordinates etc. The variables A and B are linked like this: `var A = L.geoJson(data);` ..so I guess I would have to add them with separator directly? How do I do that? – julia Apr 15 '14 at 19:29
0

Sounds like you want to merge 2 geojson files into one. According to this answer, the concat method should do it for you:

var finalObj = json1.concat(json2); // Merge the 2 files together.
Community
  • 1
  • 1
Bob Bryan
  • 3,687
  • 1
  • 32
  • 45