0

Is there anyway to copy only required values to a object from JSON string.

JSON string

{  
   "name":"John",
   "age":31,
   "city":"New York",
   "place" : "Ne"
}

On conversion of above string the result should be

Object a= {"name":"John","address":"","place":"Ne"}
Community
  • 1
  • 1
MerBas
  • 67
  • 7
  • 2
    `var newObj = { "name":otherObj.name, "address":otherObj.address }` – vsync Aug 30 '18 at 08:31
  • Parse the string to object using JSON.parse and copy specific values from it. If you are open to libraries, lodash has a way `.pick` for this – Rajesh Aug 30 '18 at 08:35
  • 3
    Possible duplicate of [How to get a subset of a javascript object's properties](https://stackoverflow.com/questions/17781472/how-to-get-a-subset-of-a-javascript-objects-properties) – Red Devil Aug 30 '18 at 08:37

1 Answers1

0

You can try to use Object destructuring. Read more here.

const oldObj = {  
   "name":"John",
   "age":31,
   "city":"New York",
   "place" : "Ne"
}
const { name, age, city } = oldObj;
const newObj = { name, age, city };
KarlR
  • 1,545
  • 12
  • 28