Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter json object based on key [duplicate]

My json object looks like this

Object
  A:Object
  B:Object
  C:Object
  D Object
  E:Object
  F:Object
  __proto__:Object

I want to get only D and E from this json object based on key. New json object should only include D and E. how to acheive this?

like image 460
Loura Avatar asked Oct 23 '25 01:10

Loura


1 Answers

Try this:

function copyObjectProps(source, keys) {
   let newObject = {}
   keys.forEach(function(key) {
     newObject[key] = source[key]
   })
   return newObject
}

And use it like:

let filteredObject = copyObjectProps(yourObject, ['D', 'E'])
like image 102
Amresh Venugopal Avatar answered Oct 24 '25 13:10

Amresh Venugopal