https://github.com/leon/similar-object
Compare objects by similar fields
https://github.com/leon/similar-object
Last synced: 10 months ago
JSON representation
Compare objects by similar fields
- Host: GitHub
- URL: https://github.com/leon/similar-object
- Owner: leon
- Created: 2022-01-27T06:32:36.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-27T21:35:34.000Z (over 4 years ago)
- Last Synced: 2025-05-27T09:32:14.911Z (about 1 year ago)
- Language: TypeScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Similar Object
A utility to compare objects with numeric fields and get back a score of how close they are.
Can then be used together with `.sort()` to get the most similar objects.
Useful for getting related objects that don't match fully.
# Install
```sh
npm install similar-object
# or
yarn add similar-object
```
```ts
import { similarObject } from 'similar-object'
// we are looking at this
const current = { price: 800, rooms: 3, area: 80 }
// which one of these is closest to current?
const unit1 = { price: 1000, rooms: 3, area: 100 }
const unit2 = { price: 1000, rooms: 5, area: 50 }
const unit3 = { price: 1500, rooms: 5, area: 50 }
const units = [unit1, unit2, unit3]
const similarConfig = {
price: 1, // weight 1
rooms: 2, // it's more important so we boost the score of rooms by 2
area: 3, // area is most important, so we boost it by 3
}
units.sort(
(a, b) => similarObject(current, a, similarConfig) - similarObject(current, b, similarConfig),
)
// units are now sorted according to how similar they are
```