Normalizr具有嵌套的对象数组

时间:2017-12-13 13:43:29

标签: javascript redux normalizr

我有一个嵌套的对象数组,如下所示:

var matchs = [
    {
      id: 10689,
      sport: 'Tennis',
      players: [
        {
        id: 22,
        name:'Rafa Nadal',
        country: 'Spain',
        odds: [
           {id: 1, bookie_1: 1.60},
           {id: 2, bookie_2: 1.61},
           {id: 3, bookie_3: 1.62},
           ]
        },
        {
        id: 23,
        name:'Roger Federer',
        country: 'Spain',
        odds: [
           {id: 4, bookie_1: 2.60},
           {id: 5, bookie_2: 2.61},
           {id: 6, bookie_3: 2.62},
          ]
        }
      ]
    },
    {
      id: 12389,
      sport: 'Tennis',
      players: [
        {
        id: 45,
        name:'Fernando Verdasco',
        country: 'Spain',
        odds: [
           {id: 7, bookie_1: 2.60},
           {id: 8, bookie_2: 2.61},
           {id: 9, bookie_3: 2.62},
          ]
        },
        {
        id: 65,
        name:'Andy Murray',
        country: 'Spain',
        odds: [
           {id: 10, bookie_1: 1.60},
           {id: 11, bookie_2: 1.61},
           {id: 12, bookie_3: 1.62},
          ]
        }
      ]
    }
  ];

我想使用normalizr来简化数组并与redux一起使用。我已经阅读了Normalizr文档,但它有一些例子,我不知道我做错了什么。

我尝试了以下代码但没有成功。我得到的结果是一个未定义的数组。

  import { normalize, schema } from 'normalizr';   

  const match = new schema.Entity('matchs');
  const player = new schema.Entity('players');
  const odd = new schema.Entity('odds');

  match.define({
    player: [player],
    odd: [odd]
  });      

  console.log(normalize(matchs, [match]));

我需要这样的东西:

{
  result: "123",
  entities: {
    "matchs": { 
      "123": { 
        id: "123",            
        players: [ "1","2" ],
        odds: [ "1", "2" ]
      }
    },
    "players": {
      "1": { "id": "1", "name": "Rafa Nadal" },
      "2": { "id": "2", "name": "Andy Murray" }
    },
    "odds": {
      "1": { id: "1", "bookie_1": "1.20" }
      "2": { id: "2", "bookie_2": "1.21" }
      "3": { id: "3", "bookie_3": "1.22" }
    }
  }
}

3 个答案:

答案 0 :(得分:0)

我认为这就是你需要的

const odd = new schema.Entity('odds');
const player = new schema.Entity('players' , { odds: [ odd]});

const match = new schema.Entity('matchs', {players: [player]});

但结果会有所不同,因为你的json的结构是这样的,我的意思是,赔率键是玩家的孩子,而不是匹配,因此结果就是这样。

只需看一下控制台

答案 1 :(得分:0)

我无法仅使用normalizr找到直接的解决方案,所以我唯一的选择是在传递给规范器之前对数据进行预格式化。

const preformattedData = data.map(sport => {
  const oddArrays = sport.players.map(player => player.odds || []);
  return {
    ...sport,
    odds: [].concat.apply([], oddArrays)
  }
})

const odd = new schema.Entity('odds')
const player = new schema.Entity('players',
  {
    odds: [ odd ]
  }
)
const sport = new schema.Entity('sports',
  {
    players: [ player ],
    odds: [odd]
  }
)

const normalizedData = normalize(preformattedData, [ sport ]);

演示:https://codesandbox.io/s/20onxowzwn

答案 2 :(得分:-1)

您可以通过调整流程和合并策略来获得所需的结果。我没有时间为您做腿部工作,但是我在这里详细解释了这种方法:

https://medium.com/@JustinTRoss/normalizing-data-into-relational-redux-state-with-normalizr-47e7020dd3c1