The dataset
function is designed to facilitate easy manipulation and analysis of data arrays. It returns an object with various methods that allow for SQL-like operations on the data. Here's a breakdown of its functionality with examples:
To create a dataset, simply call the dataset
function with an array of objects:
import { dataset } from 'your-library'
const data = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]
const ds = dataset(data)
The dataset object returned by dataset
provides the following methods:
select()
: Returns the entire dataset.where(predicate)
: Filters the dataset based on the provided predicate function.union(otherDataset)
: Performs a union operation with another dataset.intersect(otherDataset)
: Performs an intersection operation with another dataset.minus(otherDataset)
: Performs a set difference operation with another dataset.rename(mapping)
: Renames columns in the dataset using either a mapping object or a function.drop(...columns)
: Drops specified columns from the dataset.update(updates)
: Updates records in the dataset based on the provided updates object or function.delete()
: Deletes records from the dataset based on optional conditions.fillNA(fillValues)
: Fills null or undefined values in the dataset with provided values.sortBy(...columns)
: Sorts the dataset based on specified columns.
Let's explore some examples of using these methods:
const filteredData = ds.where((row) => row.id > 1).select()
const renamedData = ds.rename({ id: 'userID', name: 'fullName' }).select()
const updatedData = ds.update({ name: 'New Name' }).select()
const sortedData = ds.sortBy('name').select()
const newData = [{ id: 3, name: 'Charlie' }]
const unionData = ds.union(newData).select()
const intersectData = ds.intersect(newData).select()
const minusData = ds.minus(newData).select()
Refer to the following additional documentation for more details on specific operations: