lodash group按键嵌套对象的数组

时间:2022-08-08 13:18:47

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:

现在,我想将具有类似“route_id”的值分组到嵌套的对象数组中;最终使用lodash;以便预期结果如下:

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.

我尝试使用_.group但我不知道如何在一个对象下获得嵌套数组。另请注意,还有其他键和“属性”键,但它们与结构无关。

2 个解决方案

#1


1  

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.

如果features集合中的对象包含一个键属性,那么我强烈建议规范化结构,删除属性键。不过,这是您可能正在寻找的实现。

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

函数参考: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>

#2


0  

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

在jquery中你可以使用$ .extend(obj1,obj2);

#1


1  

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.

如果features集合中的对象包含一个键属性,那么我强烈建议规范化结构,删除属性键。不过,这是您可能正在寻找的实现。

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

函数参考: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>

#2


0  

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

在jquery中你可以使用$ .extend(obj1,obj2);