【问题标题】:Returning ids & its key from an object within an array从数组中的对象返回 ids 及其键
【发布时间】:2021-10-08 11:24:50
【问题描述】:

我有一个多维 javascript 对象数组,我试图用它来简单地将单元数组中的 id 及其键整理到一个全新的数组中

返回 id 的最佳解决方案是在它的单位数组中使用键但是反转所以新数组的键是单位 id

[
  {
    units: [
      {
        id: 10000282,
        name: "Group 1",
      },
      {
        id: 10000340,
        name: "Group 2",
      },
      {
        id: 10000341,
        name: "Group 3",
      },
    ],
  },
  {
    units: [
      {
        id: 10000334,
        name: "Group 4",
      },
    ],
  },
]

预期输出 - 只需返回以下格式的数组 例如

ids = [ 10000282 => 0, 10000340 => 1, 10000341 => 2, 10000334 => 0 ]

所以10000282 是键,0 是数组第一次迭代的值 - 更新 - 输出可能是我解释得不够好,输出应该如下,但是是数组格式。

ids[10000282] = 0
ids[10000340] = 1
ids[10000341] = 2
ids[10000334] = 0

【问题讨论】:

  • 你的意思是输出为一个字符串数组?
  • 一个数组输出,这样我就可以在控制台中调用ids[10000341] 之类的东西,它会返回值 2
  • 啊,听起来你希望结果是一个对象,而不是一个数组。

标签: javascript arrays loops dictionary foreach


【解决方案1】:

因此,我想您希望将结果返回到字典中,其中键为嵌套 id,并在包装​​单元中为其索引赋值。您可以轻松地执行以下操作:

x = [
  {
    units: [
      {
        id: 10000282,
        name: "Group 1",
      },
      {
        id: 10000340,
        name: "Group 2",
      },
      {
        id: 10000341,
        name: "Group 3",
      },
    ],
  },
  {
    units: [
      {
        id: 10000334,
        name: "Group 4",
      },
    ],
  },
];

result = x.flatMap(el => el.units.map((e,i) => ({[e.id]: i})));

console.log(result);

【讨论】:

  • 这很好,虽然我刚刚意识到这会返回一个从 0 开始的索引(我可能第一次没有很好地解释这一点) - 我改变了原始问题中描述的后半部分- 我如何修改您的解决方案,使其成为简单的数组,我可以调用数组的第一个索引,如 ids[10000340] 并且返回 1
【解决方案2】:

这是使用reduce 的稍微不同的方法:

const data = [{
    units: [{
        id: 10000282,
        name: "Group 1",
      },
      {
        id: 10000340,
        name: "Group 2",
      },
      {
        id: 10000341,
        name: "Group 3",
      },
    ],
  },
  {
    units: [{
      id: 10000334,
      name: "Group 4",
    }, ],
  },
];

const result = data.reduce(
  (total, current) =>
    total.concat(current.units.map(({ id }, i) => ({ [id]: i }))),
  []
);

console.log(result);

【讨论】:

  • 使用 reduce 的好例子,我会尝试它们,谢谢
【解决方案3】:

听起来您希望能够直接访问指向重构为对象或Mapid 属性。

使用使用Object.fromEntries()创建的对象

const arr = [{ units: [{ id: 10000282, name: "Group 1", }, { id: 10000340, name: "Group 2", }, { id: 10000341, name: "Group 3", },], }, { units: [{ id: 10000334, name: "Group 4", },], },];

const result = Object.fromEntries(arr.flatMap(({ units }) => units.map(({ id }, i) => [id, i])));

console.log(result);
// { '10000282': 0, '10000334': 0, '10000340': 1, '10000341': 2 }

console.log('result[10000340] = ', result[10000340])
// result[10000340] = 1

使用地图

const arr = [{ units: [{ id: 10000282, name: "Group 1", }, { id: 10000340, name: "Group 2", }, { id: 10000341, name: "Group 3", },], }, { units: [{ id: 10000334, name: "Group 4", },], },];

const result = new Map(arr.flatMap(({ units }) => units.map(({ id }, i) => [id, i])));
// Map(4) { 10000282 => 0, 10000340 => 1, 10000341 => 2, 10000334 => 0 }

console.log('result.get(10000340) = ', result.get(10000340))
// result.get(10000340) =  1

【讨论】:

  • 哦,天哪,代码太棒了——这就是我需要的,抱歉我没有解释清楚——但这太棒了@pilchard,这正是我想要实现的目标。感谢所有让我来到这里的人:)
  • 感谢您提供了这两个示例 - 我最后使用了地图 :)
【解决方案4】:

const arrays = [
  {
    units: [
      {
        id: 10000282,
        name: "Group 1",
      },
      {
        id: 10000340,
        name: "Group 2",
      },
      {
        id: 10000341,
        name: "Group 3",
      },
    ],
  },
  {
    units: [
      {
        id: 10000334,
        name: "Group 4",
      },
    ],
  },
];

const results = arrays.flatMap((items) => items.units.map(({id}, index) => `ids[${id}] = ${index}`));

console.log(...results);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    相关资源
    最近更新 更多