2017-07-05 2 views
0

Ich habe dieses Array:Underscore.js - Gruppenobjekte von Eigentum und ausdrucken

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, ..." 
    }]; 

und ich brauche es in einer Art und Weise zu einer Gruppe, die den Abschnitt nach ihm alle Eigenschaften enthält, die den gleichen Abschnitt haben, wie folgt:

[ 
    {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.']} 
] 

So, wenn ich es drucke, wird es nur den Text jedes Abschnitts drucken.

Dies ist der Code, den ich bisher habe:

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); 

ich versucht habe:

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

aber noch wird es nichts drucken. In meinen Entwicklungswerkzeugen zeigt es nichts.

irgendwelche Tipps?

Antwort

1

Sie können es ohne Unterstrich:

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);

Oder mit Strich:

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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

+0

es muss mit Strich erfolgen:/ – noel293

+0

@GeorgeXReplay. Ich habe eine Unterstreichungsversion meines Javascript-Codes hinzugefügt. –