如何基于带有数组的对象创建新对象

时间:2019-05-11 11:31:17

标签: javascript arrays reactjs react-native ecmascript-6

我有一个带有目录的全局保存数组,以及用户为他保存的项目列表。 我只需要用客户端保存的项目创建一个新的对象数组(带有数组)。 我在react-native中使用javascript,但我认为javascript会尽可能地具有全局性。

我已经成功过滤了“类别”,并使用array.map和array.filter过滤了图形,但是我确实需要再次形成基本结构,以便可以重用组件来呈现列表。

全局数组

const catalog = [
    {
        title: "Masculine T-Shirts 2017",
        code:"1101",
        page:"http://www.blablabla.com/catalog/1101/",
        data: [[
            {
                code:"1101-1",
                name:"Masculine T-Shirt product 1",
                page:"http://www.blablabla.com/catalog/1101/product/1101-1/",
                description:"description of product 1",
             },
             {
                code:"1101-2",
                name:"Masculine T-Shirt product 2",
                page:"http://www.blablabla.com/catalog/1101/product/1101-2/",
                description:"description of product 2",
             },
             {
                code:"1101-3",
                name:"Masculine T-Shirt product 3",
                page:"http://www.blablabla.com/catalog/1101/product/1101-3/",
                description:"description of product 3",
             }
        ]]
    },
    {
        title: "Masculine T-Shirts 2018",
        code:"1203",
        page:"http://www.blablabla.com/catalog/1203/",
        data: [[
            {
                code:"1203-1",
                name:"Masculine T-Shirt product 1",
                page:"http://www.blablabla.com/catalog/1203/product/1203-1/",
                description:"description of product 1",
             },
             {
                code:"1203-2",
                name:"Masculine T-Shirt product 2",
                page:"http://www.blablabla.com/catalog/1203/product/1203-2/",
                description:"description of product 2",
             },
             {
                code:"1203-3",
                name:"Masculine T-Shirt product 3",
                page:"http://www.blablabla.com/catalog/1203/product/1203-3/",
                description:"description of product 3",
             },
             {
                code:"1203-4",
                name:"Masculine T-Shirt product 4",
                page:"http://www.blablabla.com/catalog/1203/product/1203-4/",
                description:"description of product 4",
             }
        ]]
    },
    {
        title: "Masculine socks 2018",
        code:"1304",
        page:"http://www.blablabla.com/catalog/1304/",
        data: [[
            {
                code:"1304-1",
                name:"Masculine socks product 1",
                page:"http://www.blablabla.com/catalog/1304/product/1304-1/",
                description:"description of socks product 1",
             },
             {
                code:"1304-2",
                name:"Masculine socks product 2",
                page:"http://www.blablabla.com/catalog/1304/product/1304-2/",
                description:"description of socks product 2",
             }
        ]]
    },
]

用户保存的列表:

SavedList = ["1304-2","1101-3","1304-2"];

我需要过滤返回的数组,但是格式与原始数组相同,例如:

savedCatalog = [
    {
        title: "Masculine T-Shirts 2017",
        code:"1101",
        page:"http://www.blablabla.com/catalog/1101/",
        data: [[
             {
                code:"1101-3",
                name:"Masculine T-Shirt product 3",
                page:"http://www.blablabla.com/catalog/1101/product/1101-3/",
                description:"description of product 3",
             }
        ]]
    },
    {
        title: "Masculine socks 2018",
        code:"1304",
        page:"http://www.blablabla.com/catalog/1304/",
        data: [[
            {
                code:"1304-1",
                name:"Masculine socks product 1",
                page:"http://www.blablabla.com/catalog/1304/product/1304-1/",
                description:"description of socks product 1",
             },
             {
                code:"1304-2",
                name:"Masculine socks product 2",
                page:"http://www.blablabla.com/catalog/1304/product/1304-2/",
                description:"description of socks product 2",
             }
        ]]
    },
]

1 个答案:

答案 0 :(得分:0)

这是您执行此操作的一种方法,您认为使用了地图和过滤器。使用仅包含搜索项的Object.assign(因此,我们不直接更改目录常量中的内容)构造一个新的目录项,然后为搜索结果多于零的那些过滤新构建的目录项。

y