I need to convert a js object to another object for passing onto a server post where the names of the keys differ for example
JavaScript
x
10
10
1
var a = {
2
name : "Foo",
3
amount: 55,
4
reported : false,
5
6
<snip/>
7
8
date : "10/01/2001"
9
}
10
needs to turn into
JavaScript
1
10
10
1
a = {
2
id : "Foo",
3
total : 55,
4
updated: false,
5
6
<snip/>
7
8
issued : "10/01/2001"
9
}
10
where I have lookup obj available for mapping all the keys
JavaScript
1
8
1
var serverKeyMap = {
2
name : "id",
3
amount : "total",
4
reported : "updated",
5
6
date : "issue"
7
}
8
Is there a function available in underscore.js or jQuery that I can use that does this functionality?
thanks
Advertisement
Answer
As far as I know there is no function built into either of these two libraries. You can make your own fairly easily, though: http://jsfiddle.net/T9Lnr/1/.
JavaScript
1
7
1
var b = {};
2
3
_.each(a, function(value, key) {
4
key = map[key] || key;
5
b[key] = value;
6
});
7