【问题标题】:Push multiple array fields into separate arrays将多个数组字段推送到单独的数组中
【发布时间】:2021-08-16 05:04:44
【问题描述】:

我有一个要求,我有这种格式的数据。我怎样才能或最好和最容易获得以下格式的输出?

users = [
{
id: "1",
name: "Norah"
},
{
id: "2",
name: "Alyx"
}
];
accounts = [
{
account: "IRA-4679",
user: "1",
balance: "5175.36"
},
{
account: "AAA-3571",
user: "1",
balance: "3106701.85"
},
{
account: "AAA-4671",
user: "1",
balance: "138971.19"
},
{
account: "ROT-1687",
user: "2",
balance: "2686.00"
},
{
account: "AAA-7894",
user: "2",
balance: "68761.32"
},
{
account: "IRA-6818",
user: "2",
balance: "564.67"
},
{
account: "IRA-6819",
user: "2",
balance: "6564.67"
}
];

输出

["Norah | AAA-6818 | 564.67","Norah | AAA-4671 | 138971.19"]

是否与拆分数组并根据 id 合并为一个有关,或者我应该在这里做什么?

【问题讨论】:

  • 你的输出不正确,能更新一下吗?

标签: javascript arrays sorting arraylist


【解决方案1】:

const users=[{id:"1",name:"Norah"},{id:"2",name:"Alyx"}];
const accounts=[{account:"IRA-4679",user:"1",balance:"5175.36"},{account:"AAA-3571",user:"1",balance:"3106701.85"},{account:"AAA-4671",user:"1",balance:"138971.19"},{account:"ROT-1687",user:"2",balance:"2686.00"},{account:"AAA-7894",user:"2",balance:"68761.32"},{account:"IRA-6818",user:"2",balance:"564.67"},{account:"IRA-6819",user:"2",balance:"6564.67"}];

const res = accounts.map(e => [
  users.find(x => x.id === e.user).name,
  e.account,
  e.balance
].join(" | "))

console.log(res)

【讨论】:

  • 如何根据姓名或账户余额在输出中应用排序?
【解决方案2】:

users = [
  {
    id: "1",
    name: "Norah",
  },
  {
    id: "2",
    name: "Alyx",
  },
];

accounts = [
  {
    account: "IRA-4679",
    user: "1",
    balance: "5175.36",
  },
  {
    account: "AAA-3571",
    user: "1",
    balance: "3106701.85",
  },
  {
    account: "AAA-4671",
    user: "1",
    balance: "138971.19",
  },
  {
    account: "ROT-1687",
    user: "2",
    balance: "2686.00",
  },
  {
    account: "AAA-7894",
    user: "2",
    balance: "68761.32",
  },
  {
    account: "IRA-6818",
    user: "2",
    balance: "564.67",
  },
  {
    account: "IRA-6819",
    user: "2",
    balance: "6564.67",
  },
];

let finalOutput = users
  .sort((a, b) => (a.name > b.name ? 1 : -1))
  .map((user) =>
    accounts
      .filter((account) => account.user == user.id)
      .sort((a, b) => (parseFloat(a.balance) > parseFloat(b.balance) ? -1 : 1))
      .map(
        (filteredAccount) =>
          user.name +
          " | " +
          filteredAccount.account +
          " | " +
          filteredAccount.balance
      )
  );

console.log(finalOutput);

这应该可以解决问题,对我来说,运行上面的 sn-p 会将您的两个输入变成一个由两个数组组成的数组,其形状如下:

["Alyx | AAA-7894 | 68761.32", "Alyx | IRA-6819 | 6564.67", "Alyx | ROT-1687 | 2686.00", "Alyx | IRA-6818 | 564.67"]

["Norah | AAA-3571 | 3106701.85", "Norah | AAA-4671 | 138971.19", "Norah | IRA-4679 | 5175.36"]

编辑:根据要求,我现在添加了排序,首先按名称排序,然后按帐户余额从大到小降序排序。

导致上面看到的更新输出。

【讨论】:

  • 如何根据姓名或账户余额在输出中应用排序?
  • @KunalVijan 我现在添加了排序,首先按用户名排序,然后按 DESC 帐户余额从最大帐户余额到最小帐户余额排序。如果你想扭转这种情况,只需将过滤操作之后的第二个排序函数更改为 .sort((a, b) => (parseFloat(a.balance) > parseFloat(b.balance) ? 1 : -1)
【解决方案3】:

创建用户的Map,然后map() 覆盖帐户并使用 destructuringtemplate literal 返回您的字符串。

const
  users = [{ id: "1", name: "Norah" }, { id: "2", name: "Alyx" }],
  accounts = [{ account: "IRA-4679", user: "1", balance: "5175.36" }, { account: "AAA-3571", user: "1", balance: "3106701.85" }, { account: "AAA-4671", user: "1", balance: "138971.19" }, { account: "ROT-1687", user: "2", balance: "2686.00" }, { account: "AAA-7894", user: "2", balance: "68761.32" }, { account: "IRA-6818", user: "2", balance: "564.67" }, { account: "IRA-6819", user: "2", balance: "6564.67" }],

  userMap = new Map(users.map(({ id, name }) => [id, name])),
  result = accounts.map(({ user, account, balance }) =>
    `${userMap.get(user)} | ${account} | ${balance}`);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

如果您需要保持访问用户/帐户元素的各个属性的能力,最好将它们保留为对象。您可以将用户名合并到帐户数组中并定义一个toString() 函数,以便轻松地将单个 userAccount 对象转换为字符串,或根据需要将整个数组映射为字符串。

const
  users = [{ id: "1", name: "Norah" }, { id: "2", name: "Alyx" }],
  accounts = [{ account: "IRA-4679", user: "1", balance: "5175.36" }, { account: "AAA-3571", user: "1", balance: "3106701.85" }, { account: "AAA-4671", user: "1", balance: "138971.19" }, { account: "ROT-1687", user: "2", balance: "2686.00" }, { account: "AAA-7894", user: "2", balance: "68761.32" }, { account: "IRA-6818", user: "2", balance: "564.67" }, { account: "IRA-6819", user: "2", balance: "6564.67" }],

  userAccountToString = (acct) => `${acct.user} | ${acct.account} | ${acct.balance}`,
  userMap = new Map(users.map(({ id, name }) => [id, name])),
  userAccounts = accounts.map(({ user, ...account }) =>
    ({ user: userMap.get(user), ...account }));

// accounts with user name merged
console.log(userAccounts);
console.log(userAccounts.map(userAccountToString));

// sort as needed
console.log('\nBy balance:')
userAccounts.sort((a, b) => a.balance - b.balance);
console.log(userAccounts);
// output array as account strings
console.log(userAccounts.map(userAccountToString));

// sort as needed
console.log('\nBy user:')
userAccounts.sort((a, b) => a.user.localeCompare(b.user));
console.log(userAccounts);
// output array as account strings
console.log(userAccounts.map(userAccountToString));
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 如何根据姓名或账户余额在输出中应用排序?
  • 如果你想保持对单个 props 的访问,你最好将它们保留为对象并定义一个 toString() 方法,以便在需要时输出所需的字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
相关资源
最近更新 更多