gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:56:27 25 4
gpt4 key购买 nike

我有这个数组:

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)

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

有什么建议吗?

最佳答案

您可以不使用下划线:

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

或者,使用下划线:

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>

关于javascript - underscorejs - 按属性对对象进行分组并打印它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44923342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com