2
我正在使用normalizr。我嘗試正常化字符串,但我沒有弄清楚。是否可以normalizr字符串?
const mails = [
{
"id": "mailId1",
"conversationId": "conversationId1",
"content": "Hi",
"sender": {
"emailAddress": { name: 'Jack', address: '[email protected]' }
}
},
{
"id": "mailId2",
"conversationId": "conversationId1",
"content": "Hello",
"sender": {
"emailAddress": { name: 'Rose', address: '[email protected]' }
}
}
]
const userSchema = new schema.Entity('users', {}, {
idAttribute: value => value.emailAddress.address
});
const conversationIdSchema = new schema.Entity('conversationIds', {}, {
idAttribute: value => value
});
const mailSchema = new schema.Entity('mails', {
conversationId: conversationIdSchema,
sender: userSchema
});
const normalizedData = normalize(mails, [mailSchema]);
現在上面的代碼給我下面的結果:
(注意:conversationIdSchema
不工作,因爲它是錯誤的)
{
result: { mails: ['mailId1', 'mailId2'] },
entities: {
users: {
'[email protected]': { name: 'Jack', address: '[email protected]' }
'[email protected]': { name: 'Rose', address: '[email protected]' }
},
mails: {
// ...
}
}
}
我希望能有這樣的conversationIds:
{
result: { mails: ['mailId1', 'mailId2'] },
entities: {
users: {
'[email protected]': { name: 'Jack', address: '[email protected]' }
'[email protected]': { name: 'Rose', address: '[email protected]' }
},
mails: {
// ...
},
conversationIds: ['conversationId1']
}
}