Last active
June 30, 2016 18:31
-
-
Save himynameisdave/856d3f690c19784480b206bd967f13e8 to your computer and use it in GitHub Desktop.
specify an array of keys you want and an array of objs, should return an array of objects with only specified keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const objects = [ | |
| { | |
| a: 'a', | |
| b: 'b', | |
| c: 'c' | |
| },{ | |
| a: 'a', | |
| b: 'b', | |
| c: 'c' | |
| },{ | |
| a: 'a', | |
| b: 'b', | |
| c: 'c' | |
| } | |
| ]; | |
| // I only want these keys | |
| const keysIWant = ['a', 'b']; | |
| const getSanitizedObjects = () => { | |
| // Expected: | |
| // getSanitizedObjects[0] -> { a: 'a', b: 'b' } | |
| // naive approach: | |
| return objects.map(obj => { | |
| return { | |
| a: obj.a, | |
| b: obj.b | |
| } | |
| }) | |
| }; |
jasonbellamy
commented
Jun 30, 2016
Author
@jasonbellamy returns an array of arrays....
womp womp womp thanks for playing try again next time.
A solution that worked:
const extract = (x, xs) => {
return x.map(y => {
const obj = {};
xs.map(z => {
obj[z] = y[z];
});
return obj;
});
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
