I have a javascript object with key/value pairs where the value is an array of strings:
var errors = {
"Message": ["Error #1", "Error #2"],
"Email": ["Error #3", "Error #4"]
};
I would like to convert this to a key/value pair with the first element of each array so that it is a instead of :
var firstErrorOnly = {
"Message": "Error #1",
"Email": "Error #3"
};
I tried doing this using jQuery.map but I was not getting the results I wanted. How can I accomplish this?
You can traverse the whole object using
for ... inandhasOwnProperty()to create a new object in the style you wish.