这可能是一个很简单的问题,但在最近一个小时里我还没有找到答案。 我有以下数组,它总是在索引0中包含单个对象。 为了方便以后使用,我想把对象从数组中移除,直接作为单个对象存储。 即。 对象不应再包装在数组中。
当前状态:数组
Array(1)
0:
bio: "Test"
id: 2
image: "http://localhost:8000/media/default.jpg"
user: 2
目标:对象
Object
bio: "Test"
id: 2
image: "http://localhost:8000/media/default.jpg"
user: 2
你可以简单地用一个扩展运算符来完成。
null
const arr = [{
bio: "Test",
id: 2,
image: "http://localhost:8000/media/default.jpg",
user: 2
}]
const obj = {...arr[0]}
console.log(obj)