I need to normalize this data so that I have both an array of lists and another of todos.
const data = [
{
_id: '1',
title: 'List1',
todos: [
{
_id: "11",
text: "Test1"
}
]
},
{
_id: '2',
title: 'List2',
todos: [
{
_id: "22",
text: "Test2"
}
]
}
];
Here's what I got:
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);
I've been trying their examples but none of them seem to work for this data.
Any help would be appreciated.
You need to tell the schema that todos
is an array of todo
and that your input data is an array:
const list = new schema.Entity('lists', { todos: [ todo ]}, { idAttribute: '_id' });
const normalizedData = normalize(data, [ list ]);
or
const list = new schema.Entity('lists', { todos: new schema.Array(todo) } , { idAttribute: '_id' });
const normalizedData = normalize(data, new schema.Array(list));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With