Javascript Object.keys在数组es6中复制对象
我有这样一个对象结构:Javascript Object.keys在数组es6中复制对象,javascript,arrays,object,ecmascript-6,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
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
将此对象转换为数组,但最终在一个数组中有两个对象。一个对象包含内部数组字符
,另一个对象不包含数组字符
。我得到了这样的东西
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
]
对象被复制。一次使用嵌套数组,另一次不使用嵌套数组。我只是不想要这个复制品。
var objs={
vKzGBlblU7hVqIildUIb:{
人物:[{
姓名:“,
id:“
}],
键:“vKzGBlblU7hVqIildUIb”,
创建人:“test1234”,
已删除位置:null,
体裁:“,
主要字符:“,
字符数:2,
状态:“草稿”,
摘要:“摘要23”,
标题:“第23篇”,
上传地址:1531686836601
},
irufsxKuw9I20pLDa6P7:{
人物:[{
姓名:“,
id:“
}],
密钥:“irufsxKuw9I20pLDa6P7”,
创建人:“test1234”,
已删除位置:null,
体裁:“,
主要字符:“,
字符数:2,
状态:“草稿”,
摘要:“摘要12”,
标题:“提香23434”,
上传地址:1531686836601
}
}
常量数组=Object.keys(objs.map)((obj,index)=>{
返回obj[obj]
});
console.log(数组)代码>您可以使用,或者:
const obj={vkzgbblblu7hvqiilduib:{字符:[{name:,id:}],键:“vkzgbblu7hvqiilduib},irufsxKuw9I20pLDa6P7:{字符:[{name:,id:}],键:“irufsxKuw9I20pLDa6P7};
constv1=Object.keys(obj.map)(k=>obj[k]);
常数v2=对象值(obj);
constv3=Object.entries(obj.map)([[uo])=>o;
console.log(v1);
console.log(v2);
console.log(v3)
您可以只使用Object.values
或Object.entries
而不是向我们显示一个真实的对象,而不是一个无法测试的控制台表示。也显示了预期的结果。请参见更新的POSI在阵列中推送相同对象两次。错别字,是我的错。看起来我需要一杯咖啡:(