首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用嵌套对象数组的Normalizr

使用嵌套对象数组的Normalizr
EN

Stack Overflow用户
提问于 2017-12-13 21:43:29
回答 4查看 1.7K关注 0票数 3

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

代码语言:javascript
复制
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文档,但它的示例很少,我不知道我做错了什么。

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

代码语言:javascript
复制
  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]));

我需要这样的东西:

代码语言:javascript
复制
{
  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" }
    }
  }
}
EN

回答 4

Stack Overflow用户

发布于 2018-11-03 01:45:19

我不能只使用normalizr找到直接的解决方案,所以我唯一的选择是在传递给规范化程序之前预先格式化数据。

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2017-12-14 16:30:54

我想这就是你所需要的

代码语言:javascript
复制
const odd = new schema.Entity('odds');
const player = new schema.Entity('players' , { odds: [ odd]});

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

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

只需看一下控制台

票数 0
EN

Stack Overflow用户

发布于 2021-01-15 16:48:17

以下是使用最新版本的标准化的解决方案

代码语言:javascript
复制
const odds = new schema.Entity("odds");
const players = new schema.Entity("players", {
  odds: [odds]
});
const matches = new schema.Entity("matches", { players: [players] });
const normalizedData = normalize(data, [matches]);

它会将问题中的数据分组为

代码语言:javascript
复制
{
 "entities": {
    "odds": {
        "1": {
            "id": 1,
            "bookie_1": 1.6
        }
    },
    "players": {
        "22": {
            "id": 22,
            "name": "Rafa Nadal",
            "country": "Spain",
            "odds": [
                1,
                2,
                3
            ]
        }
    },
    "matches": {
        "10689": {
            "id": 10689,
            "sport": "Tennis",
            "players": [
                22,
                23
            ]
        }
    }
},
"result": [
    10689
]

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47794469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档