将对象数组变成对象

Eli*_*han 3 javascript arrays object ecmascript-6

我有一个分支数组,看起来像这样:

let branches = [
  {
    id: 21,
    name: "Branch 1",
    opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
  },
  {
    id: 22,
    name "Branch 2"
    opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
  },
  // .. etc
]
Run Code Online (Sandbox Code Playgroud)

但我想将其变成一个对象,每个对象的名称均作为关键字。

所需的输出:

branches = {
  "Branch 1": {
    id: 21,
    opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
  },
  "Branch 2": {
    id: 22,
    opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
  }
}
Run Code Online (Sandbox Code Playgroud)

尝试过:

branches = {
  "Branch 1": {
    id: 21,
    opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
  },
  "Branch 2": {
    id: 22,
    opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
  }
}
Run Code Online (Sandbox Code Playgroud)

但是当然映射给了我一个数组输出:

let newBranches = branches.map(branch => (
  {
    [branch.name]: {
      id: branch.id,
      days: branch.opening_times
    }
  }
));
console.log(newBranches)
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我指出正确的方向,以将name密钥作为对象本身来获得一个新对象吗?

T.J*_*der 6

我只是使用一个简单的for-of循环。您会得到reduce答案,但是reduce这里所做的只是增加了复杂性。

const result = {};
for (const {name, id, opening_times} of branches) {
  result[name] = {id, opening_times};
}
Run Code Online (Sandbox Code Playgroud)

现场示例:

const result = {};
for (const {name, id, opening_times} of branches) {
  result[name] = {id, opening_times};
}
Run Code Online (Sandbox Code Playgroud)
let branches = [
  {
    id: 21,
    name: "Branch 1",
    opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
  },
  {
    id: 22,
    name: "Branch 2",
    opening_times: [ {}, {}, {} ] // Array of objects (Monday, Tuesday etc)
  },
  // .. etc
];
const result = {};
for (const {name, id, opening_times} of branches) {
  result[name] = {id, opening_times};
}
console.log(result);
Run Code Online (Sandbox Code Playgroud)


补充Code Maniac关于使用休息的建议

const result = {};
for (const {name, ...entry} of branches) {
  result[name] = entry;
}
Run Code Online (Sandbox Code Playgroud)

现场示例:

.as-console-wrapper {
    max-height: 100% !important;
}
Run Code Online (Sandbox Code Playgroud)
const result = {};
for (const {name, ...entry} of branches) {
  result[name] = entry;
}
Run Code Online (Sandbox Code Playgroud)

两者略有不同,因为第一个显式仅使用idopening_times结果,而其余版本使用以外的所有属性name。当然,可读性有所不同(显式与隐式),但是我会在每个地方都使用它们。

  • ***在旁注:-***我想我们可以在这里使用`rest parameter`而不是取出所有属性然后重新创建对象,即`for(分支的const {name,... rest}) ` (4认同)
  • 与reduce方法相比,更喜欢可读性。有什么想法会更快吗? (2认同)
  • @EliNathan-在现代JavaScript引擎上,我希望这是一个洗礼。“ for-of”创建并使用一个迭代器(要进行优化,JavaScript引擎肯定可以做到),该迭代器涉及函数调用。`reduce`不使用迭代器,但是有对回调的调用。所以... (2认同)

Rob*_*sen 6

通过简单的reduce()操作和对象分解:

const branches = [{
    id: 21,
    name: "Branch 1",
    opening_times: []
  },
  {
    id: 22,
    name: "Branch 2",
    opening_times: []
  }
];

const result = branches.reduce((a, {name, ...v}) => (a[name] = v, a), {});

console.log(result);
Run Code Online (Sandbox Code Playgroud)