我有一个要复制的伪造数据对象,该怎么做?

时间:2019-05-04 13:48:59

标签: javascript reactjs

我有一个伪造的数据对象user,如下所示:

const user = {
    _id: String,
    name: String,
    userUploads: Array,
};

如何在user中复制userOne以获得类似的内容

const userOne = {
    _id: 'dddddd',
    name: 'Chioma',
    userUploads: [
        {
            id: 0,
            // eslint-disable-next-line max-len
            image: 'http://sugarweddings.com/files/styles/width-640/public/1.%20The%20Full%20Ankara%20Ball%20Wedding%20Gown%20@therealrhonkefella.PNG',
            reactions: {
                dislike: 0,
                like: 0,
                maybe: 0,
            },
            story: "It's my birthday next week! What do you think?",
        },
        {
            id: 1,
            // eslint-disable-next-line max-len
            image: 'https://dailymedia.com.ng/wp-content/uploads/2018/10/7915550_img20181007141132_jpeg01c125e1588ffeee95a6f121c35cd378-1.jpg',
            reactions: {
                dislike: 0,
                like: 0,
                maybe: 0,
            },
            story: 'What do you think about this?',
        },
        {
            id: 2,
            // eslint-disable-next-line max-len
            image: 'https://i0.wp.com/www.od9jastyles.com/wp-content/uploads/2018/01/ankara-styles-ankara-styles-gown-ankara-tops-ankara-gowns-ankara-styles-pictures-latest-ankara-style-2018-latest-ankara-styles-ankara-ankara-styles.png?fit=437%2C544&ssl=1',
            reactions: {
                dislike: 0,
                like: 0,
                maybe: 0,
            },
            story: 'Saturdays are for weddings. Yay or nay?',
        },
    ],
};

1 个答案:

答案 0 :(得分:2)

您可以使用传播运算符执行以下操作:

let userOne = {...user};

但是请注意,这将是shallow副本。