简体   繁体   中英

How to Convert multiple arrays of objects to single object

i have arrays like below:

 [{"1":"test1"},{"2":"test2"},{"3":"test3"}]

I want to convert it like below:

 [{"1":"test1","2":"test2","3":"test3"}]

I want result as a single object.please provide solution for this.

One quick way is to use Object.assign and spread operator

 let arr = [{"1":"test1"},{"2":"test2"},{"3":"test3"}] let obj = Object.assign({},...arr); console.log(obj);

Doc: Object.assign , spread operator

 var arr = [{"1":"test1"},{"2":"test2"},{"3":"test3"}]; var newarr = arr.reduce(function(data,val){ Object.assign(data[0],val); return data; },[{}]); console.log(newarr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM