2

I have an object, I would like to flatten that using jquery / underscore. Any one help me the shortest way to do this?

Here is my actual object :

var testObj = {
    "name": "Tester",
    "age": "32",
    "address": {
        "street1": "abc",
        "houseObj": {
            "houseName": "somename"
        }
    },
    "marks": [
        {
            "eng": 80
        },
        {
            "maths": 98
        }
    ]
};

I am looking the output is :

var flattenedObj = {
                "name": "Tester",
                "age": "32",
                "address.street1": "abc",
                "address.houseObj.houseName": "somename",
                "marks.0.end": 80,
                "marks.1.maths": 98 
}

Any one give me the short way to do this?

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • 1
    Don't really need underscore for that, here's a gist: https://gist.github.com/penguinboy/762197 (not mine, but I've used it as a basis for utilities) – zamnuts Jun 30 '14 at 06:50

0 Answers0