https://zhuanlan.zhihu.com/p/55984381
许多API,都会返回一个深层的嵌套的对象。在js应用中使用这样的结构通常比较困难,尤其是用在flux以及redux中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "id": "123", "author": { "id": "1", "name": "Paul" }, "title": "My awesome blog post", "comments": [ { "id": "324", "commenter": { "id": "2", "name": "Nicole" } } ] }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
import { normalize, schema } from 'normalizr';
const user = new schema.Entity('users');
const comment = new schema.Entity('comments', { commenter: user });
const article = new schema.Entity('articles', { author: user, comments: [comment] });
const normalizedData = normalize(originalData, article);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| { result: "123", entities: { "articles": { "123": { id: "123", author: "1", title: "My awesome blog post", comments: [ "324" ] } }, "users": { "1": { "id": "1", "name": "Paul" }, "2": { "id": "2", "name": "Nicole" } }, "comments": { "324": { id: "324", "commenter": "2" } } } }
|