【问题标题】:Normalize linked ids规范化链接的 id
【发布时间】:2018-03-17 19:25:54
【问题描述】:

我这里有一组奇怪的数据。

const data = {
    profiles: [
        { name: 'Joe', photos: [1, 2, 3] },
        { name: 'Ryan', photos: [2] },
        { name: 'Bob', photos: null }
    ],
    linked: {
        photos: [
            { id: 1, url: 'http://blah' },
            { id: 2, url: 'blah' },
            { id: 3, url: 'asdf' }
        ]
    }
}

我得到所有这样的个人资料:

const { entities } = normalize(data, {
    profiles: [ Profile ]
});

但是我想用来自linked.photos 的条目替换photos id 数组,这可能吗?还是需要后期处理?我目前正在进行自定义后期处理。

【问题讨论】:

    标签: normalizr


    【解决方案1】:

    我不确定 normalizr 是完成任务的最佳方法,但这样的方法会起作用

    const photoSchema = new schema.Entity('photos', {});
    const normalized = normalize(data.linked.photos, [photoSchema]);
    const profileRawSchema = new schema.Entity('profiles', {}, {idAttribute: 'name'})
    const profileSchema = new schema.Entity('profiles', {
        photos: [photoSchema]
    }, {idAttribute: 'name'});
    const normalizedProfiles = normalize(
        data.profiles,
        [profileRawSchema]
    );
    
    normalized.entities.profiles = normalizedProfiles.entities.profiles;
    // here is what you want
    const desiredResult = denormalize(normalizedProfiles.result, [profileSchema], normalized.entities);
    

    Docs for denormalize

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 2017-10-26
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      • 2021-05-27
      相关资源
      最近更新 更多