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

时间:2018-07-19 02:08:18

标签: javascript arrays object ecmascript-6

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

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将此对象转换为数组,但最终在数组中包含2个对象。一个包含内部数组characters,另一个对象不包含数组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
}

我期望Object.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 个答案:

答案 0 :(得分:0)

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

这是怎么了?

答案 1 :(得分:0)

您可以使用Object.keysObject.valuesObject.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);