diff
Returns a new array containing elements from the first array that are not present in the second. For arrays of objects, pass an iteratee function to derive an identity value used for comparison.
Try it
Section titled “Try it”Import
Section titled “Import”import { diff } from "1o1-utils";import { diff } from "1o1-utils/diff";Signature
Section titled “Signature”function diff<T>({ array, values, iteratee }: DiffParams<T>): T[]Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| array | T[] | Yes | The source array to filter |
| values | T[] | Yes | The values to exclude from the source array |
| iteratee | (item: T) => unknown | No | Function returning the identity value used to compare |
Returns
Section titled “Returns”T[] — A new array containing elements from array not present in values.
Examples
Section titled “Examples”diff({ array: [1, 2, 3, 4], values: [2, 4] });// => [1, 3]
diff({ array: [{ id: 1 }, { id: 2 }, { id: 3 }], values: [{ id: 2 }], iteratee: (item) => item.id,});// => [{ id: 1 }, { id: 3 }]Edge Cases
Section titled “Edge Cases”- Throws if
arrayis not an array. - Throws if
valuesis not an array. - Without
iteratee, items are compared with SameValueZero (Setsemantics) —NaNequalsNaN,+0equals-0. - With
iteratee, mapped values are compared with the same SameValueZero semantics; the function is called for every element of both arrays. - Errors thrown by
iterateepropagate to the caller. - Order of
arrayis preserved. - Duplicates in
arraythat are not invaluesare kept. - Inputs are not mutated.
Also known as
Section titled “Also known as”difference, except, exclude, subtract arrays, set difference
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use diff to remove a list of items from an array of objects by id.