代码之家  ›  专栏  ›  技术社区  ›  Kamil Kamili

Object.keys复制数组es6中的对象

  •  0
  • Kamil Kamili  · 技术社区  · 7 年前

    我有一个这样的对象结构:

    vKzGBlblU7hVqIildUIb:
    characters: (2) [{…}, {…}]
    created_by: "test1234"
    deleted_at: null
    genre: ""
    key: "vKzGBlblU7hVqIildUIb"
    main_character: ""
    number_of_characters: 2
    status: "draft"
    summary: "Summary 23"
    title: "Titile 23"
    uploaded_at: 1531686836601
    

    我试图用 Object.keys 但我最终在一个数组中有两个对象。一个包含内部数组的 characters 另一个不包含数组的对象 字符 是的。我得到这样的东西。

    0:
        characters: (2) [{…}, {…}]
        created_by: "test1234"
        deleted_at: null
        genre: ""
        key: "vKzGBlblU7hVqIildUIb"
        main_character: ""
        number_of_characters: 2
        status: "draft"
        summary: "Summary 23"
        title: "Titile 23"
        uploaded_at: 1531686836601
    
    1:
        created_by: "test1234"
        deleted_at: null
        genre: ""
        key: "vKzGBlblU7hVqIildUIb"
        main_character: ""
        number_of_characters: 2
        status: "draft"
        summary: "Summary 23"
        title: "Titile 23"
        uploaded_at: 1531686836601
    

    这就是我试图将对象转换为数组的方式。

    const array = Object.keys(objs).map((obj, index) => {
          return objs[obj]
        });
    

    我不明白我做错了什么有什么建议吗?

    更新: 对象结构很简单:

    {
        vKzGBlblU7hVqIildUIb: {
            characters: [{
                name: "",
                id: ""
            }]
            key: "vKzGBlblU7hVqIildUIb"
            //other props listed in the example above
        }
        irufsxKuw9I20pLDa6P7: {
            characters: [{
                name: "",
                id: ""
            }]
            key: "irufsxKuw9I20pLDa6P7"
            //other props listed in the example above
        }
        // so on
    }
    

    我所期望的 对象.keys 可以做到:

    [
        0: {
            characters: [{
                name: "",
                id: ""
            }]
            key: "vKzGBlblU7hVqIildUIb"
            //other props listed in the example above
        }
        1: {
            characters: [{
                name: "",
                id: ""
            }]
            key: "irufsxKuw9I20pLDa6P7"
            //other props listed in the example above
        }
    
        // so on
    ]
    

    但实际上发生了什么:

    [
        0: {
            characters: [{
                name: "",
                id: ""
            }]
            key: "vKzGBlblU7hVqIildUIb"
            //other props listed in the example above
        }
        1: {
            characters: [{
                name: "",
                id: ""
            }]
            key: "irufsxKuw9I20pLDa6P7"
            //other props listed in the example above
        }
        2: {
            key: "vKzGBlblU7hVqIildUIb"
            //other props listed in the example above
        }
        3: {
            key: "irufsxKuw9I20pLDa6P7"
            //other props listed in the example above
        }
        // so on
    ]
    

    对象被复制一次使用嵌套数组,另一次不使用。我只是不想要这个复制品。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Hardik Shah    7 年前

    var objs = {
        vKzGBlblU7hVqIildUIb: {
            characters: [{
                name: "",
                id: ""
            }],
            key: "vKzGBlblU7hVqIildUIb",
            created_by: "test1234",
            deleted_at: null,
            genre: "",
            main_character: "",
            number_of_characters: 2,
            status: "draft",
            summary: "Summary 23",
            title: "Titile 23",
            uploaded_at: 1531686836601
        },
        irufsxKuw9I20pLDa6P7: {
            characters: [{
                name: "",
                id: ""
            }],
            key: "irufsxKuw9I20pLDa6P7",
            created_by: "test1234",
            deleted_at: null,
            genre: "",
            main_character: "",
            number_of_characters: 2,
            status: "draft",
            summary: "Summary 12",
            title: "Titile 23434",
            uploaded_at: 1531686836601
        }
    }
    
    
    const array = Object.keys(objs).map((obj, index) => {
          return objs[obj]
        });
        
        console.log(array);

    这里怎么了?

        2
  •  0
  •   Leonid Pyrlia    7 年前

    你可以用它 Object.keys 我是说, Object.values Object.entries 以下内容:

    const obj={vKzGBlblU7hVqIildUIb:{characters:[{name:"",id:""}],key:"vKzGBlblU7hVqIildUIb"},irufsxKuw9I20pLDa6P7:{characters:[{name:"",id:""}],key:"irufsxKuw9I20pLDa6P7"}};
    
    const v1 = Object.keys(obj).map(k => obj[k]);
    
    const v2 = Object.values(obj);
    
    const v3 = Object.entries(obj).map(([_,o]) => o);
    
    console.log(v1);
    console.log(v2);
    console.log(v3);