2017-02-03 4 views
2

Ich muss diese Daten zu normalisieren, so dass ich sowohl ein Array von Listen und ein anderes von Todos haben.Verschachtelte Arrays in Normalizr Schema

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

Hier ist, was ich habe:

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); 

Ich habe ihre Beispiele versucht, aber keiner von ihnen scheinen für diese Daten zu arbeiten.

Jede Hilfe wäre willkommen.

+0

Wo ist 'redux' und' react'? – lux

Antwort

4

Sie müssen das Schema sagen, dass todos ein Array von todo ist und dass Ihre Eingangsdaten ist ein Array:

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

oder

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