How to format an object of objects

Ndx :

I have this object

myObject = {
  id: "44",
  name: "name",
  firstName: "fn",
  lastName: "tt",
  cars: [],
  houses: [],
  family: {}
}

I want to format this object to

myObject = {
  person: {
    id: "44",
    name: "name",
    firstName: "fn",
    lastName: "tt"
  },
  cars: [],
  houses: [],
  family: {}
}

Is there anyway i can do this without using delete?

messerbill :

You can use 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)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=391057&siteId=1