Javascript 是否可以使用模板将值从一个对象映射到另一个具有不同结构的对象?

Javascript 是否可以使用模板将值从一个对象映射到另一个具有不同结构的对象?,javascript,object,Javascript,Object,我有一个API响应,如下所示: const apiResponse = { entries: [ { title: "Entry Title", url_title: "entry-title", status: "open", created_date: "14 Apr 2017", category: { cat_na

我有一个API响应,如下所示:

const apiResponse = {
    entries: [
        {
            title: "Entry Title",
            url_title: "entry-title",
            status: "open",
            created_date: "14 Apr 2017",
            category: {
                cat_name: "Information",
                cat_url_title: "information"
            },
            channel_name: "Entries",
            content: {
                text_position: "textBottomRight",
                large_image: {
                    image: "https://placeimg.com/2732/752/nature/grayscale",
                    width: 2732,
                    height: 752
                },
                small_image: {
                    image: "https://placeimg.com/1500/716/nature/grayscale",
                    width: 1500,
                    height: 716
                },
                content: "A short summary goes *here*.",
                feature_theme: {
                    theme: "themePositive",
                    background_color: "rgb(27, 27, 36)",
                    eyebrow_color: "rgb(255, 255, 255)",
                    headline_color: "rgb(255, 255, 255)",
                    text_color: "rgb(255, 255, 255)",
                    link_color: "rgb(77, 175, 255)"
                },
                enable_favoriting: 0,
                related_article: "",
                continue_link_text: "",
                large_image_height: 0,
                large_image_width: 0,
                small_image_width: 0,
                small_image_height: 0
            },
            id: 614
        }, {
            … more entries
        }
    ]
};
根据请求的“通道”,此响应的键和结构略有不同。目前有三种不同的通道,每种通道的结果都只是一个稍微不同的结构和一些可能在其他响应中不存在的不同键。无论数据源的结构如何,所有响应都需要在前端简化为相同的格式

所以我想知道,当结构不同时,是否有推荐的方法将值从一个对象映射到另一个对象?

我最初的想法是为每个频道创建一个“模板”。比如:

const channelOne = {
    entries: {
        title: 'entries.title',
        slug: 'entries.url_title',
        status: 'entries.status',
        date: 'entries.created_date',
        category: 'entries.category.cat_name',
        name: 'entries.channel_name',
        content: {
            format: 'entries.content.text_position',
            image: {
                large: 'entries.content.large_image.image',
                small: 'entries.content.small_image.image'
            }
        },
        … etc.
    }
};
我会为每个“频道”创建一个。然后,对于每个响应,我将使用该模板将响应中的值映射到该模板中的键。我还没有真正了解这将如何实际工作,因此我晚上访问了SO

有什么建议吗?

有点相关:有点相关: