合并数组中的重复对象并组合每个对象的子数组

merge duplicate objects in an array and combine sub array of each object

我正在尝试根据 Id 合并对象,并合并每个 account(对象)内的每个数组,但代码不会合并 accountList 的内容,而是覆盖数组,如果有是一个匹配的id。

我创建了一个新数组并使用 .find 方法根据那里的 ID 找到匹配的对象,但仍然坚持如何将 accountList 合并在一起

const accounts = [
    {
    "Id": 103,
    "accountList": [
      {}
    ]
  },
  {
    "Id": 103,
    "accountList": [
      {
        "tokenId": "5aasdasdsdnjn3434nadd",
        "featureId": 2840
      }
    ]
  },
  {
    "Id": 112,
    "accountList": [
      {
        "tokenId": "5d30775bef4a722c38aefaaa",
        "featureId": 2877
      }
    ]
  },
    {
    "Id": 112,
    "accountList": [
      {
        "tokenId": "5d30775bef4a722c38aefccc",
        "featureId": 2856
      }
    ]
  }
]

let result = [];
accounts.forEach(account => {
  let match = result.find(r => r.Id === account.Id);
  // console.log(match)
  if(match) {
   Object.assign(match, account);
    //tried using spread operator instead of object assign, but didnt work
    //  match = {...match, ...account}
  } else {
    result.push(account);
  }
});

console.log( JSON.stringify(result, null, 2))

我需要的结果是根据对象的 id 合并对象,并将 accountList 的内容合并在一起,如下所示:

[
  {
    "Id": 103,
    "accountList": [
      {
        "tokenId": "5aasdasdsdnjn3434nadd",
        "featureId": 2840
      }
    ]
  },
  {
    "Id": 112,
    "accountList": [
      {
        "tokenId": "5d30775bef4a722c38aefaaa",
        "featureId": 2877
      },
      {
        "tokenId": "5d30775bef4a722c38aefccc",
        "featureId": 2856
      }
    ]
  }
]

我认为你可以使用 match.accountList.push(...account.accountList); 而不是对象分配,展开运算符可用于将元素推入结果项(match):

let accounts = [{ "Id": 103, "accountList": [{}] }, { "Id": 103, "accountList": [{ "tokenId": "5aasdasdsdnjn3434nadd", "featureId": 2840 }] }, { "Id": 112, "accountList": [{ "tokenId": "5d30775bef4a722c38aefaaa", "featureId": 2877 }] }, { "Id": 112, "accountList": [{ "tokenId": "5d30775bef4a722c38aefccc", "featureId": 2856 }] }];
let result = [];
accounts.forEach(account => {
  (match = result.find(r => r.Id === account.Id), match ? match.accountList.push(...account.accountList) : result.push(account))
});
console.log(result);

使用Array.prototype.reduce我们可以将结果累加到最终的result数组中。

在 reduce 回调中,只需使用 Id 找到匹配的对象并合并 accountList 数组,而不是像您在代码中所做的那样合并对象。

const accounts=[{"Id":103,"accountList":[{}]},{"Id":103,"accountList":[{"tokenId":"5aasdasdsdnjn3434nadd","featureId":2840}]},{"Id":112,"accountList":[{"tokenId":"5d30775bef4a722c38aefaaa","featureId":2877}]},{"Id":112,"accountList":[{"tokenId":"5d30775bef4a722c38aefccc","featureId":2856}]}];

const result = accounts.reduce((acc, account) => {
     let match = acc.find(r => r.Id === account.Id);
     if(match) {
       match.accountList.push(...account.accountList); //push previous array
     } else {
       const act = { ...account }; 
       act.accountList = account.accountList.filter((obj) => Object.keys(obj).length);
       acc.push(act);
     }
     return acc;
}, []);
console.log(result);

我认为,reduce() 可以胜任:

const accounts = [{"Id":103,"accountList":[{}]},{"Id":103,"accountList":[{"tokenId":"5aasdasdsdnjn3434nadd","featureId":2840}]},{"Id":112,"accountList":[{"tokenId":"5d30775bef4a722c38aefaaa","featureId":2877}]},{"Id":112,"accountList":[{"tokenId":"5d30775bef4a722c38aefccc","featureId":2856}]}];

const result = [...accounts
        .reduce((r, o) => {
          const record = r.get(o.Id)||{}
          r.set(o.Id, {
            Id: o.Id,
            accountList: [
              ...(record.accountList||[]),
              ...o.accountList.filter(o => 
                Object.keys(o).length != 0)
            ]            
          })
          return r
        }, new Map())
        .values()]

console.log(result);
.as-console-wrapper {min-height: 100%}

你可以尝试使用Array.concat:

    let result = [];
    accounts.forEach(account => {
      let match = result.find(r => r.Id === account.Id);
      // console.log(match)
      if(match) {
        match.accountList = match.accountList.concat(account.accountList);
      } else {
        result.push(account);
      }
    });

for (let res of result) {
  console.log('res.Id: ', res.Id, res.accountList)
}

// res.Id:  103 [ {}, { tokenId: '5aasdasdsdnjn3434nadd', featureId: 2840 } ]
// res.Id:  112 [ { tokenId: '5d30775bef4a722c38aefaaa', featureId: 2877 },
//   { tokenId: '5d30775bef4a722c38aefccc', featureId: 2856 } ]
const isNotEmptyObject = objc => Object.entries(objc).length > 0;

function mergeAccounts(accounts) {
    const uniqueAccounts = new Map();
    accounts.forEach(account => {
        if(uniqueAccounts.has(account.Id)) {
            let uniqueAccount = uniqueAccounts.get(account.Id);
            if(account.accountList && account.accountList.length > 0)
                uniqueAccount.accountList.push(...account.accountList);
                uniqueAccount.accountList = uniqueAccount.accountList.filter(isNotEmptyObject);
        } else {
            uniqueAccounts.set(account.Id, account);
        }
    });
  return Array.from(uniqueAccounts.values());
}

这将合并所有具有相同 ID 的帐户。希望这会有所帮助:)