2017-02-03 23 views
2

我需要規範化這些數據,以便我有一個列表數組和另一個待辦事項列表。normalizr模式中的嵌套數組

const data = [ 
    { 
    _id: '1', 
    title: 'List1', 
    todos: [ 
     { 
     _id: "11", 
     text: "Test1" 
     } 
    ] 
    }, 
    { 
    _id: '2', 
    title: 'List2', 
    todos: [ 
     { 
     _id: "22", 
     text: "Test2" 
     } 
    ] 
    } 
]; 

這裏就是我的了:

const todo = new schema.Entity('todos',{},{ idAttribute: '_id'}); 
const list = new schema.Entity('lists',{todos:todo},{idAttribute: '_id'}); 
const normalizedData = normalize(data, list); 
console.log(normalizedData); 

我一直想自己的例子,但沒有人似乎爲這一數據的工作。

任何幫助,將不勝感激。

+0

哪裏'redux'和'react'? – lux

回答

4

你需要告訴模式,它todostodo和輸入數據的陣列是:

const list = new schema.Entity('lists', { todos: [ todo ]}, { idAttribute: '_id' }); 
const normalizedData = normalize(data, [ list ]); 

const list = new schema.Entity('lists', { todos: new schema.Array(todo) } , { idAttribute: '_id' }); 
const normalizedData = normalize(data, new schema.Array(list));