我正在尝试合并共享相同模块密钥值的对象,即“信用卡”。我需要为每个唯一的模块返回一个对象,其中包含一系列不同的公司。 id和prod键并不重要。
这是我需要转换的对象:
const data = [
{module: "credit_cards", id: "1", company: "ABC", prod: "1"},
{module: "credit_cards", id: "2", company: "XYZ", prod: "2"},
{module: "credit_cards", id: "3", company: "EFG", prod: "2"},
{module: "credit_cards", id: "2", company: "XYZ", prod: "2"},
{module: "mortgages", id: "4", company: "EFG", prod: "3"}
]
我需要返回以下内容:
const result = [
{module: "credit_cards", company: ["ABC", "XYZ", "EFG"]},
{module: "mortgages", company: ["EFG"]}
]
到目前为止,我已经尝试了此功能:
const result = data.reduce(function(r, e) {
return Object.keys(e).forEach(function(k) {
if(!r[k]) r[k] = [].concat(e[k])
else r[k] = r[k].concat(e[k])
}), r
}, {})
但是它只是将所有键/值连接在一起...
任何帮助都会很棒,谢谢:)
答案 0 :(得分:3)
您可以使用reduce
将数组汇总为Map
。将Set
用作唯一值。
const data = [{"module":"credit_cards","id":"1","comapany":"ABC","prod":"1"},{"module":"credit_cards","id":"2","comapany":"XYZ","prod":"2"},{"module":"credit_cards","id":"3","comapany":"EFG","prod":"2"},{"module":"credit_cards","id":"2","comapany":"XYZ","prod":"2"},{"module":"mortgages","id":"4","comapany":"EFG","prod":"3"}]
const result = Array.from(data.reduce((c, {module,comapany}) => {
if (!c.has(module)) c.set(module, new Set);
c.get(module).add(comapany);
return c;
}, new Map)).map(([module, comapany]) => ({module,comapany: [...comapany]}));
console.log(result);
答案 1 :(得分:3)
简化为以module
索引的对象,然后获取该对象的值。因为只需要唯一的company
值,所以请先使用Set,然后再将所有项的company
集转换回数组:
const data = [
{module: "credit_cards", id: "1", company: "ABC", prod: "1"},
{module: "credit_cards", id: "2", company: "XYZ", prod: "2"},
{module: "credit_cards", id: "3", company: "EFG", prod: "2"},
{module: "credit_cards", id: "2", company: "XYZ", prod: "2"},
{module: "mortgages", id: "4", company: "EFG", prod: "3"}
]
const result = Object.values(data.reduce(
(a, { module, company }) => {
if (!a[module]) a[module] = { module, company: new Set() };
a[module].company.add(company);
return a;
},
{}
));
result.forEach((item) => {
item.company = [...item.company];
});
console.log(result);
答案 2 :(得分:3)
您可以先使用Array.reduce()通过Set上的companies
键将module
分组。第二步,您可以对生成的对象array.Map()使用.entries()以获得最终的期望结果:
const data = [
{module: "credit_cards", id: "1", company: "ABC", prod: "1"},
{module: "credit_cards", id: "2", company: "XYZ", prod: "2"},
{module: "credit_cards", id: "3", company: "EFG", prod: "2"},
{module: "credit_cards", id: "2", company: "XYZ", prod: "2"},
{module: "mortgages", id: "4", company: "EFG", prod: "3"}
];
let res = data.reduce((acc, {module, company}) =>
{
acc[module] = acc[module] || new Set();
acc[module].add(company);
return acc;
}, {})
res = Object.entries(res).map(
([module, companies]) => ({module, company: [...companies]})
);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
答案 3 :(得分:0)
这里是使用Array.reduce并进行内部检查以确保公司列表唯一且没有重复的另一种方法。
const data = [ {module: "credit_cards", id: "1", company: "ABC", prod: "1"}, {module: "credit_cards", id: "2", company: "XYZ", prod: "2"}, {module: "credit_cards", id: "3", company: "EFG", prod: "2"}, {module: "credit_cards", id: "2", company: "XYZ", prod: "2"}, {module: "mortgages", id: "4", company: "EFG", prod: "3"} ]
let result = data.reduce((r,{module, company}) => {
r[module] = r[module] || { module, company: [] }
if(!r[module].company.includes(company))
r[module].company.push(company)
return r
}, {})
console.log(Object.values(result))
答案 4 :(得分:0)
const data = [
{module: 'credit_cards', id: '1', company: 'ABC', prod: '1'},
{module: 'credit_cards', id: '2', company: 'XYZ', prod: '2'},
{module: 'credit_cards', id: '3', company: 'EFG', prod: '2'},
{module: 'credit_cards', id: '2', company: 'XYZ', prod: '2'},
{module: 'mortgages', id: '4', company: 'EFG', prod: '3'}
];
const result = data.reduce((array, item) => {
const id = array.findIndex(el => el.module === item.module);
if (id > -1) {
array[id] = {...array[id], company : [...array[id].company, item.company]};
return array;
}
return [...array, {...item, company : [ item.company]}];
}, []);