Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
'Moving' properties from an object into a new object, based on a 3rd object

I have an object which contains multiple properties. For a given list of props in a 3rd object, I want to 'move' the properties from the original object and put them into a new object.

I have a 'longhand' way of doing this at the moment, something like the following. However this creates 2 new objects, rather than just 'moving' some of the keys out:

let pets = {
  'cat': {},
  'dog': {},
}

let animals = {
  'cat': 'black',
  'dog': 'white',
  'whale': 'blue',
  'albatross': 'grey',
}

let aPet = {}
let notAPet = {}
for (let [key, val] of Object.entries(animals)) {
  if (key in pets) {
    aPet[key] = val
  } else {
    notAPet[key] = val
  }
}

Is there a nicer 'short' way of achieving the above, preferably only creating aPet while making animals end up equivalent to notAPet?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use delete operator:

let pets = {
  'cat': {},
  'dog': {},
}

let animals = {
  'cat': 'black',
  'dog': 'white',
  'whale': 'blue',
  'albatross': 'grey',
}

let aPet = {}
let notAPet = {}
for (let [key, val] of Object.entries(animals)) {
  if (key in pets) {
    aPet[key] = val
    delete animals[key]
  }
}

console.log(aPet, animals)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!