3 回答
TA贡献1798条经验 获得超7个赞
您可以使用destructuring:
const myObject = { id:"44", name:"name", firstName:"fn", lastName:"tt", cars: [], houses:[], family:{}}
const { houses, cars, family, ...rest } = myObject
const myNewObject = {
person: rest,
houses,
cars,
family
}
console.log(myNewObject)
TA贡献1831条经验 获得超10个赞
您可以使用解构赋值将您的对象分解为变量并在一个简单的函数中重构它们。
const format = (obj) => {
const {id, name, firstName, lastName, ...props} = obj;
return {person: {id, name, firstName, lastName}, ...props}
}
const formatted = format(myObject);
console.log (formatted)
<script>
var myObject = { id:"44", name:"name", firstName:"fn", lastName:"tt", cars: [], houses:[], family:{}}
</script>
TA贡献1831条经验 获得超9个赞
您可以解构您需要的内容,将其余部分放入变量rest中,然后重新分配myObject:
let myObject = {
id: "44",
name: "name",
firstName: "fn",
lastName: "tt",
cars: [],
houses: [],
family: {}
}
const {id, name, firstName, lastName, ...rest } = myObject;
myObject = {
person: { id, name, firstName, lastName },
...rest
}
console.log(myObject);
添加回答
举报
