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);
添加回答
舉報