normalizr模式中的嵌套数组

时间:2017-02-03 16:02:56

标签: javascript 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);

我一直在尝试他们的例子,但他们似乎都没有为这些数据工作。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:7)

您需要告诉架构Method 1: Positive -1.11022302462516e-16 Method 1: Negative -1.0 Method 2: Positive -1e-16 Method 2: Negative -0.900719925474099 Subtractive cancellation error for Method 1: Method 1 (positive) minus Method 2 (positive) -1.10223024625156e-17 Subtractive cancellation error for Method 2: Method 1 (negative) minus Method 2 (negative) -0.0992800745259007 是一个todos数组,并且您的输入数据是一个数组:

todo

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