Javascript 过滤数组中的数据并返回调整后的数组

Javascript 过滤数组中的数据并返回调整后的数组,javascript,arrays,typescript,ecmascript-6,Javascript,Arrays,Typescript,Ecmascript 6,我有这样一个数组: const arr = [ { name: 'A', accounts: [a, b]}, { name: 'B', accounts: [c, d]}, { name: 'C', accounts: [e, f]}, { name: 'A', accounts: [g, h]}, { name: 'B', accounts: [i, j

我有这样一个数组:

const arr = [
              { name: 'A', accounts: [a, b]},
              { name: 'B', accounts: [c, d]},
              { name: 'C', accounts: [e, f]},
              { name: 'A', accounts: [g, h]},
              { name: 'B', accounts: [i, j]}
];

const arr = [
              { name: 'A', accounts: [a, b, g, h]},
              { name: 'B', accounts: [c, d, i, j]},
              { name: 'C', accounts: [e, f]}
]
我想修改这个数组如下:

const arr = [
              { name: 'A', accounts: [a, b]},
              { name: 'B', accounts: [c, d]},
              { name: 'C', accounts: [e, f]},
              { name: 'A', accounts: [g, h]},
              { name: 'B', accounts: [i, j]}
];

const arr = [
              { name: 'A', accounts: [a, b, g, h]},
              { name: 'B', accounts: [c, d, i, j]},
              { name: 'C', accounts: [e, f]}
]
这意味着将帐户项目与具有相同名称的对象组合

我就是这样尝试的:

returnGroupedArr = (arr) => {
              const result = arr.reduce((f, s) => {
                           f[s.name] = s[f.name];
                           f[s.name].push(s);
                           return f;
              }
}
你能帮我找出我错过了什么吗?
谢谢。

您在每次迭代中都无条件地重新分配了
名称
的累加器属性

为了解决这个问题,并避免对输入进行变异,如果名称还不存在,请为其创建一个新数组,然后将其推入。然后通过映射其
对象将其转换回一个对象数组。条目

const arr=[
{名称:'A',帐户:['A','b']},
{名称:'B',帐户:['c','d']},
{名称:'C',帐户:['e','f']},
{名称:'A',帐户:['g','h']},
{名称:'B',帐户:['i','j']}
];
const groupedObj=arr.reduce((a,{name,accounts})=>{
如果(!a[名称]){
a[名称]=[];
}
a[名称]。推送(…帐户);
返回a;
}, {});
const group=Object.entries(groupedObj)
.map(([name,accounts])=>({name,accounts}));

控制台日志(分组)
您在每次迭代中都无条件地重新分配
名称
的累加器属性

为了解决这个问题,并避免对输入进行变异,如果名称还不存在,请为其创建一个新数组,然后将其推入。然后通过映射其
对象将其转换回一个对象数组。条目

const arr=[
{名称:'A',帐户:['A','b']},
{名称:'B',帐户:['c','d']},
{名称:'C',帐户:['e','f']},
{名称:'A',帐户:['g','h']},
{名称:'B',帐户:['i','j']}
];
const groupedObj=arr.reduce((a,{name,accounts})=>{
如果(!a[名称]){
a[名称]=[];
}
a[名称]。推送(…帐户);
返回a;
}, {});
const group=Object.entries(groupedObj)
.map(([name,accounts])=>({name,accounts}));
控制台日志(分组)
const数据=[
{名称:'A',帐户:[“A”,“b”]},
{名称:'B',帐户:[“c”,“d”]},
{姓名:'C',账户:[“e”,“f”]},
{名称:'A',帐户:[“g”,“h”]},
{名称:'B',帐户:[“i”,“j”]}
];
const newData=data.reduce((累计、当前、i)=>{
const atIndex=accum.findIndex((entry)=>entry.name==curr.name);
如果(i!==atIndex&&atIndex!==-1){
const mergedAccounts=acum[atIndex].accounts.concat(curr.accounts);
返回Object.assign([],acum,{[atIndex]:{…acum[atIndex],accounts:mergedAccounts}})
}
返回累计浓度(当前);
}, []);
console.log(newData)
const数据=[
{名称:'A',帐户:[“A”,“b”]},
{名称:'B',帐户:[“c”,“d”]},
{姓名:'C',账户:[“e”,“f”]},
{名称:'A',帐户:[“g”,“h”]},
{名称:'B',帐户:[“i”,“j”]}
];
const newData=data.reduce((累计、当前、i)=>{
const atIndex=accum.findIndex((entry)=>entry.name==curr.name);
如果(i!==atIndex&&atIndex!==-1){
const mergedAccounts=acum[atIndex].accounts.concat(curr.accounts);
返回Object.assign([],acum,{[atIndex]:{…acum[atIndex],accounts:mergedAccounts}})
}
返回累计浓度(当前);
}, []);
console.log(newData)