https://github.com/zhangchiqing/join-by-index
A function that joins two arrays with index fields
https://github.com/zhangchiqing/join-by-index
Last synced: 2 months ago
JSON representation
A function that joins two arrays with index fields
- Host: GitHub
- URL: https://github.com/zhangchiqing/join-by-index
- Owner: zhangchiqing
- License: mit
- Created: 2016-11-19T15:57:28.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-22T22:36:39.000Z (over 8 years ago)
- Last Synced: 2024-10-14T13:18:35.833Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# join-by-index
A function that joins two arrays with index fields## Usage:
```javascript
var R = require('ramda');
var joinByIndex = require('join-by-index');// [User]
var users = [
{ id: 1, name: 'userA' },
{ id: 2, name: 'userB' },
{ id: 3, name: 'userC' },
];// [Photo]
var photos = [
{ userId: 1, photo: ' :) ' },
{ userId: 3, photo: ' :( ' },
{ userId: 2, photo: ' :-) ' },
{ userId: 2, photo: ' :-/ ' },
{ userId: 4, photo: ' :-)) ' },
];// (User?, Photo) -> PhotoWithUserName
var makePhotoWithUserName = function(user, photo) {
return {
userId: photo.userId,
username: user ? user.name : null,
photo: photo.photo,
};
};// [PhotoWithUserName]
var photosWithUserName = joinByIndex(
R.prop('id'),
R.prop('userId'),
makePhotoWithUserName,
users,
photos
);console.log(photosWithUserName);
// [
// { userId: 1, photo: ' :) ', username: 'userA' },
// { userId: 3, photo: ' :( ', username: 'userC' },
// { userId: 2, photo: ' :-) ', username: 'userB' },
// { userId: 2, photo: ' :-/ ', username: 'userB' },
// { userId: 4, photo: ' :-)) ', username: null },
// ]
```## API
```
joinByIndex :: (a -> c) -> (b -> c) -> (a?, b) -> d -> [a] -> [b] -> [d]
```