如何将Object的数组转换为key和value作为数组的Object



我正在寻找使用简化函数将对象数组转换为具有值的对象作为数组的优化解决方案

const companies=[
{name:'Company One',email:'trishabh885@gmail.com',phone:'8860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'X Name',email:'grfgr@gmail.com',phone:'8860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'A one',email:'grfgt@yahoo.com',phone:'943548213', category:'Lecture',start:1981,end:2003,date:'21-06-2009'},
{name:' Name',email:'gtrgt@gmail.com',phone:'9656013653', category:'Finance',start:1981,end:2003},
{name:'Company Name',email:'grgtrgg@gmail.com', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'sdksdjd',email:'bfbrg@gbmail.com',phone:'7485788475898548', category:'Sciennce',start:1981,end:2003,date:'21-06-2009'},
{name:'kdjfkf',email:'gfggfb@gmail.com',phone:'0959898860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdjf',email:'g5rgf@gmail.com',phone:'050498860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdjvk',email:'trishabh885@gmail.com',phone:'958958860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdkv',email:'trishabh885@gmail.com',phone:'5434886013821543543', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'zeeshan',email:'trishabh885@gmail.com',phone:'8864535213', category:'Finance',start:1981,end:2003,date:'21-06-2009'}
]
// Looking for better and optimised solution to convert array of object into object with.  values as an array using map methods  
let reduced_object={}
let reduced_array=companies.reduce((arr,current)=>{
let entries=Object.entries(current)
entries.forEach(element=>{
if(reduced_object.hasOwnProperty(element[0])){
reduced_object[element[0]].push(element[1])
}else{
reduced_object[element[0]]=[element[1]];
}
})
return reduced_object
})
console.log(reduced_array)

<代码>

使用简单的CC_2循环,迭代并检查每个项目

// ...
const reduced_object = {};
reduced_object.name = companies.map(item => item.name);
reduced_object.email = companies.map(item => item.email);
reduced_object.phone = companies.map(item => item.phone);
// ...

我会用map代替:

// ...
const reduced_object = {};
for (let key of item_keys) {
reduced_object[key] = companies.map(item => item[key]);
}

当然,一旦您知道(或确定)每个项的键的列表,您可以将其封装在for-of循环中:

let obj = {
name: companies.map(item => item.name).filter(value => value !== undefined),
email: companies.map(item => item.email).filter(value => value !== undefined),
phone: companies.map(item => item.phone).filter(value => value !== undefined),
category: companies.map(item => item.category).filter(value => value !== undefined),
start: companies.map(item => item.start).filter(value => value !== undefined),
end: companies.map(item => item.end).filter(value => value !== undefined),
date: companies.map(item => item.date).filter(value => value !== undefined)
}

此解决方案不使用reduce:

const companies=[
{name:'Company One',email:'trishabh885@gmail.com',phone:'8860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'X Name',email:'grfgr@gmail.com',phone:'8860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'A one',email:'grfgt@yahoo.com',phone:'943548213', category:'Lecture',start:1981,end:2003,date:'21-06-2009'},
{name:' Name',email:'gtrgt@gmail.com',phone:'9656013653', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'Company Name',email:'grgtrgg@gmail.com',phone:'954543656138256', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'sdksdjd',email:'bfbrg@gbmail.com',phone:'7485788475898548', category:'Sciennce',start:1981,end:2003,date:'21-06-2009'},
{name:'kdjfkf',email:'gfggfb@gmail.com',phone:'0959898860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdjf',email:'g5rgf@gmail.com',phone:'050498860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdjvk',email:'trishabh885@gmail.com',phone:'958958860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdkv',email:'trishabh885@gmail.com',phone:'5434886013821543543', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'zeeshan',email:'trishabh885@gmail.com',phone:'8864535213', category:'Finance',start:1981,end:2003,date:'21-06-2009'}
]
const arr = companies.reduce((acc, company) => ({
...acc,
...Object.entries(company).reduce((objAcc, [key, val]) => ({
...objAcc,
[key]: [...acc?.[key] ?? [], ...val ? [val] : []]
}), {})
}), {})
console.log(arr)

编辑:添加了过滤器来删除'undefined'条目,当属性在源中缺失时。

另一个解决方案:

const companies=[
{name:'Company One',email:'trishabh885@gmail.com',phone:'8860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'X Name',email:'grfgr@gmail.com',phone:'8860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'A one',email:'grfgt@yahoo.com',phone:'943548213', category:'Lecture',start:1981,end:2003,date:'21-06-2009'},
{name:' Name',email:'gtrgt@gmail.com',phone:'9656013653', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'Company Name',email:'grgtrgg@gmail.com',phone:'954543656138256', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'sdksdjd',email:'bfbrg@gbmail.com',phone:'7485788475898548', category:'Sciennce',start:1981,end:2003,date:'21-06-2009'},
{name:'kdjfkf',email:'gfggfb@gmail.com',phone:'0959898860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdjf',email:'g5rgf@gmail.com',phone:'050498860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdjvk',email:'trishabh885@gmail.com',phone:'958958860138213', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'vdkv',email:'trishabh885@gmail.com',phone:'5434886013821543543', category:'Finance',start:1981,end:2003,date:'21-06-2009'},
{name:'zeeshan',email:'trishabh885@gmail.com',phone:'8864535213', category:'Finance',start:1981,end:2003,date:'21-06-2009'}
]
const arr = companies.reduce((acc, company) => ({
...acc,
...Object.entries(company).reduce((objAcc, [key, val]) => ({
...objAcc,
[key]: [...new Set([...acc?.[key] ?? [], ...val ? [val] : []])]
}), {})
}), {})
console.log(arr)

如果你不希望有重复的值,你也可以这样做:

[...new Set(companies.flatMap(Object.keys))]
.reduce((result, key) => {result[key] = companies.map(c => c[key]); return result;}, {})

有趣的功能路线:

PP_9第一行查找所有键。第二行创建所需的结果。但老实说,在现实生活中,我会跳过这个reduce,写一个简单的for循环。我总是更看重清晰度,而不是"功能纯度"。

相关内容

  • 没有找到相关文章

最新更新