javascript - Javascript - 将对象数组转换为对象

我有一个这样的数组:


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: {…}}


]



时间:

只需要简单的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;


}

添加使用rest的法典狂热者的建议


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;


}

这些有些不同,因为第一个只在结果中使用idopening_times,而rest版本则使用除name之外的所有属性,当然,可读性(显式与隐式)有一个区别,但是有些地方可以使用它们。

使用简单的reduce()操作和对象destructuring :


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);

ES 2019提供了Object.fromEntries的精确用途 :


result = Object.fromEntries(branches.map(({name,...rest}) => [name, rest]))



它已经在大多数浏览器中实现,polyfill很容易:


Object.fromEntries = pairs => 


 Object.assign({}, ...pairs.map(([k, v]) => ({[k]: v})));



你可以使用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; }

你可以通过name和其他对象的wanted键展开新对象来分配所有对象。


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; }


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)

...