zipWith()
zipWith<
T,R>(arrays,iteratee):R[]
Creates an array of grouped elements, applying a function to each group.
When arrays have unequal lengths, shorter arrays contribute undefined for missing indices.
Type Parametersβ
T: Tβ
The type of elements in the arrays.
R: Rβ
The type of the result.
Parametersβ
arrays: readonly (readonly T[])[]β
The arrays to process.
iteratee: (...values) => Rβ
The function to combine grouped values.
Returns: R[]β
A new array of combined values.
Sinceβ
2.0.0
Performanceβ
O(n Γ m) where n is the length of the longest array and m is the number of arrays.
Also known asβ
zip (Radashi) Β· zipWith (Lodash, es-toolkit, Remeda, Ramda, Effect) Β· β (Modern Dash, Antfu)
Exampleβ
zipWith([[1, 2], [10, 20], [100, 200]], (a, b, c) => a + b + c);
// => [111, 222]
zipWith([[1, 2, 3], [4, 5, 6]], (a, b) => a * b);
// => [4, 10, 18]
How it works?β
Combines arrays with a custom function instead of creating tuples.
Element-wise operationβ
Use Casesβ
Combine arrays with transformation πβ
Merge arrays while applying a function to each group. Perfect for calculations across parallel datasets.
const prices = [100, 200, 150];
const quantities = [2, 1, 3];
const totals = zipWith(prices, quantities, (price, qty) => price * qty);
// => [200, 200, 450]
Calculate differences between datasetsβ
Compute element-wise differences or ratios. Useful for variance analysis or comparison metrics.
const actual = [100, 150, 200];
const expected = [120, 140, 180];
const variance = zipWith(actual, expected, (a, e) => a - e);
// => [-20, 10, 20]
const percentDiff = zipWith(
actual,
expected,
(a, e) => ((a - e) / e * 100).toFixed(1) + "%"
);
// => ["-16.7%", "7.1%", "11.1%"]
Combine with custom logicβ
Merge arrays using any transformation function. Essential for complex data merging or aggregation.
const names = ["Alice", "Bob"];
const scores = [95, 87];
const grades = ["A", "B+"];
const results = zipWith(
names,
scores,
grades,
(name, score, grade) => `${name}: ${score} (${grade})`
);
// => ["Alice: 95 (A)", "Bob: 87 (B+)"]
Generate chart tooltip data from parallel arraysβ
Combine label and value arrays into tooltip-ready objects. Essential for chart libraries that store labels and data separately.
const labels = ["Jan", "Feb", "Mar", "Apr"];
const revenue = [12000, 15000, 13500, 18000];
const expenses = [8000, 9500, 10000, 11000];
const tooltipData = zipWith(labels, revenue, expenses, (label, rev, exp) => ({
label,
revenue: `$${rev.toLocaleString()}`,
expenses: `$${exp.toLocaleString()}`,
profit: `$${(rev - exp).toLocaleString()}`,
}));
// => [{ label: "Jan", revenue: "$12,000", expenses: "$8,000", profit: "$4,000" }, ...]
Interpolate between 3D keyframesβ
Compute intermediate positions between animation keyframes. Essential for smooth 3D animations and motion paths.
const startPositions = [0, 0, 0];
const endPositions = [100, 50, 200];
const lerp = (t: number) =>
zipWith(startPositions, endPositions, (start, end) => start + (end - start) * t);
lerp(0); // => [0, 0, 0]
lerp(0.5); // => [50, 25, 100]
lerp(1); // => [100, 50, 200]