underscorejs - 按属性对对象进行分组并打印它们

时间:2017-07-05 10:15:28

标签: javascript arrays underscore.js

我有这个数组:

var ty = [
    {
      "Language": "en-GB",
      "Section": "Sales",
      "ItemName": "Type",
      "Texts": "Having selected the account heading select the calculation ..."
    }, 
    {
      "Language": "en-GB",
      "Section": "Sales",
      "ItemName": "Try",
      "Texts": "This is not happenning tho ..."
    },
    {
      "Language": "en-GB",
      "Section": "Taxes",
      "ItemName": "Save",
      "Texts": "The Master Tax Table has been pre populated with the current UK, ..."
    }];

我需要按照一节的方式对它进行分组,它将包含具有相同部分的所有属性,如下所示:

[
    {section: 'Sales', ItemName: ['Type', 'Try'] Texts: ["Having selected the account heading select the calculation ...", "This is not happenning tho."]},

    {section: 'Taxes', ItemName: ['Type'] Texts: ['Having selected the account heading select the calculation.']}
]

所以当我打印它时,它将只打印每个部分的文本。

这是我到目前为止的代码:

var log = function(contents) {
  if (_.isArray(contents)) {
    _.each(contents, function(e, i, l) {
      log(e);
      $('#result');
      $('#result').append('</br></br>');
    });
  } else {
    console.log(contents);
    $('#result').append(contents);
  }
};

var ty = [
{
  "Language": "en-GB",
  "Section": "Sales",
  "ItemName": "Type",
  "Texts": "Having selected the account heading select the calculation ..."
}, 
{
  "Language": "en-GB",
  "Section": "Sales",
  "ItemName": "Try",
  "Texts": "This is not happenning tho ..."
},
{
  "Language": "en-GB",
  "Section": "Taxes",
  "ItemName": "Save",
  "Texts": "The Master Tax Table has been pre populated with the current UK, ..."
}];
var out = [];
_.groupBy(ty.Section, function(item){
    section: item.Section
    _.each(ty, function(item) {
    var hold = {};
    hold.options = {};
    hold.options.section[item.ItemName] = {
        text: item.Texts,
    };
    out.push(hold)
    });
});
log(out);

我试过了:

//More code above
_.each(ty, function(item) {
    iName: item.itemName;
    var hold = {};
    hold.options = {};
    hold.options.section.iName = {
        text: item.Texts,
    };
    out.push(hold)

但它仍然不会打印任何东西。在我的开发工具中,它没有显示任何内容。

任何提示?

1 个答案:

答案 0 :(得分:1)

你可以不用下划线来做到这一点:

&#13;
&#13;
var ty = [{Language:"en-GB",Section:"Sales",ItemName:"Type",Texts:"Having selected the account heading select the calculation ..."},{Language:"en-GB",Section:"Sales",ItemName:"Try",Texts:"This is not happenning tho ..."},{Language:"en-GB",Section:"Taxes",ItemName:"Save",Texts:"The Master Tax Table has been pre populated with the current UK, ..."}];

var sections = {};
ty.forEach(o => sections[o.Section] = (sections[o.Section] || []).concat(o));

var result = Object.keys(sections).map(k => ({
    section: k, 
    ItemName: sections[k].map(s => s.ItemName), 
    Texts: sections[k].map(s => s.Texts), 
}));

console.log(result);
&#13;
&#13;
&#13;

或者,使用下划线:

&#13;
&#13;
var ty = [{Language:"en-GB",Section:"Sales",ItemName:"Type",Texts:"Having selected the account heading select the calculation ..."},{Language:"en-GB",Section:"Sales",ItemName:"Try",Texts:"This is not happenning tho ..."},{Language:"en-GB",Section:"Taxes",ItemName:"Save",Texts:"The Master Tax Table has been pre populated with the current UK, ..."}];

var result = _.chain(ty)
              .groupBy('Section')
              .map((group, k) => ({
                  section: k,
                  ItemName: _.pluck(group, 'ItemName'),
                  Texts: _.pluck(group, 'Texts'),
              }))
              .value();

console.log(result);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
&#13;
&#13;