我有一个分支数组,看起来像这样:
db.config
但是我想将其变成一个对象,每个对象的名称均为键。
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
]
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)
}
}
但是当然映射给了我一个数组输出:
let newBranches = branches.map(branch => (
{
[branch.name]: {
id: branch.id,
days: branch.opening_times
}
}
));
console.log(newBranches)
有人能用正确的方向指向我,以使用[
0: {Branch 1: {…}}
1: {Branch 2: {…}}
]
键将新对象作为对象本身吗?
答案 0 :(得分:6)
我只使用一个简单的for-of
循环。您会得到reduce
个答案,但是reduce
所做的只是增加了复杂性。
const result = {};
for (const {name, id, opening_times} of branches) {
result[name] = {id, opening_times};
}
实时示例:
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);
.as-console-wrapper {
max-height: 100% !important;
}
添加Code Maniac's suggestion的休息时间:
const result = {};
for (const {name, ...entry} of branches) {
result[name] = entry;
}
实时示例:
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, ...entry} of branches) {
result[name] = entry;
}
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
}
那些稍有不同,因为第一个明确地在结果中仅使用id
和opening_times
,而其余版本使用{{1 }}。当然,可读性有所不同(显式和隐式),但是我会在每个地方都使用它们。
答案 1 :(得分: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);
答案 2 :(得分:4)
您可以通过使用name
的通缉密钥散布新对象以及其余对象来分配所有对象。
let branches = [{ id: 21, name: "Branch 1", opening_times: [{}, {}, {}] }, { id: 22, name: "Branch 2", opening_times: [{}, {}, {}] }],
newBranches = Object.assign({}, ...branches.map(({ name, ...o }) => ({ [name]: o })));
console.log(newBranches);
.as-console-wrapper { max-height: 100% !important; top: 0; }
(即将使用)Object.fromEntries
let branches = [{ id: 21, name: "Branch 1", opening_times: [{}, {}, {}] }, { id: 22, name: "Branch 2", opening_times: [{}, {}, {}] }],
newBranches = Object.fromEntries(branches.map(({ name, ...o }) => [name, o]));
console.log(newBranches);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 3 :(得分:4)
ES 2019草案为此提供了Object.fromEntries
:
result = Object.fromEntries(branches.map(({name,...rest}) => [name, rest]))
大多数浏览器已经实现了它,但是polyfill很容易:
Object.fromEntries = iter =>
Object.assign({},
...[...iter].map(
([k, v]) => ({[k]: v})
));
答案 4 :(得分:2)
您可以使用reduce
。
let branches = [{id:21,name:"Branch 1",opening_times:[{},{},{}]},{id:22,name:"Branch 2" ,opening_times:[{},{},{}]}];
const res = branches.reduce((acc, { name, ...rest }) => (acc[name] = { ...rest }, acc), {});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
ES5语法:
var branches = [{id:21,name:"Branch 1",opening_times:[{},{},{}]},{id:22,name:"Branch 2" ,opening_times:[{},{},{}]}];
var res = branches.reduce(function(acc, curr) {
acc[curr.name] = { id: curr.id, opening_times: curr.opening_times };
return acc;
}, {});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
答案 5 :(得分:1)
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)
}
]
let newBranches = {};
branches.forEach((el) => {
newBranches[el.name] = {
id: el.id,
opening_times: el.opening_times
};
});
console.log(newBranches)
答案 6 :(得分:1)
您可以尝试使用此(ES6)
Object.assign({}, ...array.map(item => ({ [item.name]: item })));