vipin8169 vipin8169 - 3 months ago 12x
jQuery Question

Coffeescript copy the JSON object with selected keys

I have the following JSON Object which I want to copy/clone.
While cloning I want only a few specific keys, say id, measurement_tag_id and unit.

Following is my JSON object:

"id": 246,
"measurement_profile_id": 52,
"measurement_tag_id": 339,
"unit": "in",
"value": 5,
"measurement_tag": {
"id": 339,
...many more keys...

How can I achieve this in coffeescript, using only one liners?


This is very simple to do with a for loop:

new_object = {}
new_object[key] = old_object[key] for key in ["id", "measurement_tag_id", "unit"]