0

I have the following data:

  var data = {
  "features": [
    {
      "properties": {
        "route_id": 522,
        "name": "the Mekong",
        "tour_id": 538
      }
    },{
      "properties": {
        "route_id": 522,
        "name": "Cambodia & the Mekong",
        "tour_id": 545
      }
    },{
      "properties": {
        "route_id": 521,
        "name": "Cambodia",
        "tour_id": 537
      }
    }
  ]
}

Now, I want to group values that have similar "route_id" into a nested array of objects; ultimately using lodash; so that the expected result will be as following:

var result = {
  "features": [
    {
      "properties": [{
        "route_id": 522,
        "name": "the Mekong",
        "tour_id": 538
        },{
        "route_id": 522,
        "name": "Cambodia & the Mekong",
        "tour_id": 545
      }]
    },{
      "properties": {
        "route_id": 521,
        "name": "Cambodia",
        "tour_id": 537
      }
    }
  ]
}

I tried using _.group but I don't know how to get the nested array under one object. Also note that there will be other keys along with the "properties" key but they're not relevant to the structure.

Ibrahim Mohammed
  • 302
  • 5
  • 17
  • Possible duplicate of [How to group an array of objects by key](https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key) – Guruprasad J Rao Feb 26 '18 at 03:47
  • yes it does group the duplicates and i implemented the solution in the referred question but i can't get the result as a nested array under the "properties" like i show in result how it should be like – Ibrahim Mohammed Feb 26 '18 at 03:51
  • @IbrahimMohammed I advice consistency with your `properties`, right now when there are multiple values, it is an array. If it there is only one, it is an object. You might want to consider array for `properties` – Eddie Feb 26 '18 at 03:57
  • it's consistent, there's an array of features, each feature has a nested 'properties' object, this is a geojson specs but i eliminated some keys to make it simpilar – Ibrahim Mohammed Feb 26 '18 at 04:01

2 Answers2

2

If the objects in the features collection contains one key, properties, then I strongly advice to normalize the structure, removing the properties key. Nevertheless, here's an implementation that you might be looking for.

var result = {
  features: _(data.features)
    // get all routes (get rid of properties key)
    .map('properties') 
    // group by route_id
    .groupBy('route_id') 
    // place properties key again
    .map(function(group) { return { properties: group };})
    // get grouped features
    .value()
};

Function references: lodash#map, lodash#groupBy

var data = {
  "features": [{
    "properties": {
      "route_id": 522,
      "name": "the Mekong",
      "tour_id": 538
    }
  }, {
    "properties": {
      "route_id": 522,
      "name": "Cambodia & the Mekong",
      "tour_id": 545
    }
  }, {
    "properties": {
      "route_id": 521,
      "name": "Cambodia",
      "tour_id": 537
    }
  }]
};

var result = {
  features: _(data.features)
    // get all routes (get rid of properties key)
    .map('properties') 
    // group by route_id
    .groupBy('route_id') 
    // place properties key again
    .map(function(group) { return { properties: group };})
    // get grouped features
    .value()
};
  
console.log(result);
.as-console-wrapper{min-height:100%; top:0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
ryeballar
  • 29,658
  • 10
  • 65
  • 74
  • 1
    thank you, that's exactly what i needed, i appreciate your input with removing the properties key but there are other keys inside the collection. i just removed them in my snippets to make it simpler. – Ibrahim Mohammed Feb 26 '18 at 04:12
-3

In jquery you can use $.extend(obj1,obj2);

Keith Becker
  • 556
  • 1
  • 6
  • 16