最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nested arrays in normalizr schema - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Feb 3, 2017 at 17:00 Paul Armstrong 7,1861 gold badge25 silver badges36 bronze badges asked Feb 3, 2017 at 16:02 cdn34cdn34 1221 silver badge9 bronze badges 1
  • Where's redux and react? – lux Commented Feb 3, 2017 at 16:04
Add a ment  | 

1 Answer 1

Reset to default 9

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));
发布评论

评论列表(0)

  1. 暂无评论