1

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

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 ]
});

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

4

1 回答 1

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);

非规范化文档

于 2017-10-09T19:13:16.890 回答