我有一個包含物件的陣列。現在我想將陣列切片為一個新物件,該物件僅包含與某個屬性名稱匹配并按此屬性名稱分組的那些物件。問題是我也有它們之間不同的屬性名稱。名稱和 ID 在物件陣列中重復,但在新物件內部,它應該只包含一次 ID 和名稱。
原始陣列如下所示:
let personArray = [
{
id_dentist: 1,
dentist_name: 'John',
id_secretary: 6,
secretary_name: 'Paul',
id_security: 3,
security_name: 'Carl'
},
{
id_dentist: 2,
dentist_name: 'Lisa',
id_secretary: 9,
secretary_name: 'Beth',
id_security: 5,
security_name: 'Monica'
},
{
id_dentist: 1,
dentist_name: 'John',
id_secretary: 6,
secretary_name: 'Paul',
id_security: 3,
security_name: 'Carl'
}
];
新物件應如下所示:
let personObject = {
dentist: [
{ id_dentist: 1, dentist_name: 'John' },
{ id_dentist: 2, dentist_name: 'Lisa' },
],
secretary: [
{ id_secretary: 6, secretary_name: 'Paul' },
{ id_secretary: 9, secreatary_name: 'Beth' },
],
security: [
{ id_security: 3, security_name: 'Carl' },
{ id_security: 5, security_name: 'Monica' }
]
};
我很感激幫助。
根據要求,我嘗試使用reduce()and filter(),但我無法將它們拆分。這是代碼:
const obj = personArray.reduce((acc, cur) => {
const key = Object.keys(cur).filter(f => /^id_/.test(f))[0].split('_')[1];
if (!acc[key]) acc[key] = [];
acc[key].push(cur);
return acc;
}, {});
console.log(obj);
關于奇怪的資料結構,我使用SELECTSQL 語法從資料庫中獲取這些資料。
uj5u.com熱心網友回復:
給你,這可以進一步增強
let personArray = [{"id_dentist":1,"dentist_name":"John","id_secretary":6,"secretary_name":"Paul","id_security":3,"security_name":"Carl"},{"id_dentist":2,"dentist_name":"Lisa","id_secretary":9,"secretary_name":"Beth","id_security":5,"security_name":"Monica"},{"id_dentist":1,"dentist_name":"John","id_secretary":6,"secretary_name":"Paul","id_security":3,"security_name":"Carl"}];
const personObject = { dentist: [], secretary: [], security: [] };
const isExist = (arr, id, key) => arr.find(x => x[key] === id);
personArray.reduce((personObj, person) => {
const isDentistExists = isExist(personObj.dentist, person.id_dentist, 'id_dentist');
if (!isDentistExists) {
personObj.dentist.push({
id_dentist: person.id_dentist,
dentist_name: person.dentist_name
});
}
const isSecretaryExists = isExist(personObj.secretary, person.id_secretary, 'id_secretary');
if (!isSecretaryExists) {
personObj.secretary.push({
id_secretary: person.id_secretary,
secretary_name: person.secretary_name
});
}
const isSecurityExists = isExist(personObj.security, person.id_security, 'id_security');
if (!isSecurityExists) {
personObj.security.push({
id_security: person.id_security,
security_name: person.security_name
});
}
return personObj;
}, personObject);
console.log(personObject);
uj5u.com熱心網友回復:
這不是一個簡單的演算法。這是一個 [大部分] 功能實作,可以處理任意數量的 id 和名稱
let personArray = [
{
id_dentist: 1,
dentist_name: 'John',
id_secretary: 6,
secretary_name: 'Paul',
id_security: 3,
security_name: 'Carl',
},
{
id_dentist: 2,
dentist_name: 'Lisa',
id_secretary: 9,
secretary_name: 'Beth',
id_security: 5,
security_name: 'Monica',
},
{
id_dentist: 1,
dentist_name: 'John',
id_secretary: 6,
secretary_name: 'Paul',
id_security: 3,
security_name: 'Carl',
},
]
const parsed = Object.fromEntries(
Object.keys(personArray[0])
.filter(key => key.startsWith('id_'))
.map(id => {
const uniqIds = [...new Set(personArray.map(person => person[id]))]
const [, name] = id.split('_')
const matchingPeople = uniqIds.map(uniqId => {
return personArray.find(person => uniqId === person[id])
})
return matchingPeople.map(person => ({
[id]: person[id],
[`${name}_name`]: person[`${name}_name`],
}))
})
.filter(entry => entry.length > 0)
.map(groupedPeople => {
const [name] = Object.keys(groupedPeople[0])
.find(key => key.includes('_name'))
.split('_')
return [name, groupedPeople]
})
)
console.log(parsed)
uj5u.com熱心網友回復:
是的,我也承認,這并不是微不足道的。
更改您的 SQL 選擇可能是一個更好的主意,以避免接收到需要進行這些冗長轉換的冗余資料。
const arr = [
{
id_dentist: 1,
dentist_name: 'John',
id_secretary: 6,
secretary_name: 'Paul',
id_security: 3,
security_name: 'Carl'
},
{
id_dentist: 2,
dentist_name: 'Lisa',
id_secretary: 9,
secretary_name: 'Beth',
id_security: 5,
security_name: 'Monica'
},
{
id_dentist: 1,
dentist_name: 'John',
id_secretary: 6,
secretary_name: 'Paul',
id_security: 3,
security_name: 'Carl'
}
], types=Object.keys(arr[0]).reduce((a,c,k)=>{
k=c.match(/id_(.*)/);
if(k) a.push(k[1]);
return a;
},[]);
const res=Object.entries(arr.reduce((a,c)=>{
types.forEach((t,id)=>{
id="id_" t;
a[t ":" c[id]]={[id]:c[id],[t "_name"]:c[t "_name"]}
});
return a;
},{})).reduce((a,[k,o],n)=>{
[n]=k.split(":");
(a[n]=a[n]||[]).push(o)
return a;
},{});
console.log(res);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494983.html
標籤:javascript
上一篇:為什么Object.entries(array)和array.entries()回傳不同的結果?
下一篇:單擊多個鏈接時顯示多個影像
