My objective is to make an array of unique keys from a list of objects.
Example:
Input:
var input = [{"a":1, "b":2}, {"a":3, "c":4}, {"a":5}]
Output:
[ "a", "b", "c" ]
Current approach:
var outputObj={};
input.map( function( obj ) {
Object.keys( obj ).map( function(key) {
outputObj[key] = 1;
})
});
// output obj is now { a : 1, b: 1, c: 1 }
var output = Object.keys( outputObj );
This works, but it just smells funny, partly because the map function is relying on side-effects. Is there a better way to do this?
In Java, I would create a java.util.Set
and simply add all keys to it.