I have an array
of object, and want to get the unique keys by underscore, how can I do that?
Array[ Object{a: 1, b: 2}, Object{b: 3, c: 4, d: 5} ]
I want to get:
Array[ "a", "b", "c", "d" ]
I have an array
of object, and want to get the unique keys by underscore, how can I do that?
Array[ Object{a: 1, b: 2}, Object{b: 3, c: 4, d: 5} ]
I want to get:
Array[ "a", "b", "c", "d" ]
Use _.keys() to get all keys in the object
_.contains([],value) checks value is exiting in the array or not and return 'true', if it exists; 'false', otherwise.The following is for eliminating duplicated keys and then pushes keys to result array.
Pure ES6 solution using Spread operator.
Background
It takes the properties of the items (
objA
,objB
) and "unwraps" them, then it combines these properties into a single object (see{ }
). Then we take the keys of the result.TypeScript transpile output (ES5):
Another option:
Underscore solution:
Demo: http://jsbin.com/vagup/1/edit