This is making my head hurt, if there are any generous javascript gurus here, i would greatly appreciate some help
What I’m trying to achieve is this:
Given this:
var keys = ["Age", "Name", "Photos", { "Friends": ["FirstName", "LastName"] }];
var values = [ [31, "Bob", ["1.jpg", "2.jpg"], [ ["Bob", "Hope"], ["Foo", "Bar"] ] ], [21, "Jane"] ["4.jpg", "5.jpg"], [ ["Mr", "T"],["Foo", "Bar"] ] ];
I would like to get back this:
var object = [
{
"Age" : 31,
"Name" : "Bob",
"Photos" : ["1.jpg", "2.jpg"]
"Friends": [
{
"FirstName": "Bob",
"LastName" : "Hope"
},
{
"FirstName": "Foo",
"LastName" : "Bar"
}
]
},
{
"Age" : 21,
"Name" : "Jane",
"Photos" : ["4.jpg", "5.jpg"]
"Friends": [
{
"FirstName": "Mr",
"LastName" : "T"
},
{
"FirstName": "Foo",
"LastName" : "Bar"
}
]
}
];
It’s for a spec proposal (JsonR) i’m working on here
Currently i’m able to (almost) work this out (but not any deeper..):
var keys = ["Age", "Name", "Photos" ];
var values = [ [31, "Bob", ["1.jpg", "2.jpg"]], [21, "Jane", ["4.jpg", "5.jpg"]] ];
Thank’s for any feedback or help!
Here’s a function that does what I think you want:
It does not handle malformed input, so you might want to put checks in there depending on how you plan to use it.
You can see it in action on this online jsFiddle demo.
By the way, the key and value value arrays you gave had mismatched opening and closing brackets, so I had to fix them.