Skip to main content

flattenConnection
utility

The flattenConnection utility transforms a connection object from the Storefront API (for example, Product-related connections) into a flat array of nodes. The utility works with either nodes or edges.node.

If connection is null or undefined, will return an empty array instead in production. In development, an error will be thrown.

|
Was this section helpful?

number
required

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

() => string
required

Returns a string representation of an array.

() => string
required

Returns a string representation of an array. The elements are converted to string using their toLocaleString methods.

() => unknown
required

Removes the last element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.

(...items: unknown[]) => number
required

Appends new elements to the end of an array, and returns the new length of the array.

{ (...items: ConcatArray<unknown>[]): unknown[]; (...items: unknown[]): unknown[]; }
required

Combines two or more arrays. This method returns a new array without modifying any existing arrays.

(separator?: string) => string
required

Adds all the elements of an array into a string, separated by the specified separator string.

() => unknown[]
required

Reverses the elements in an array in place. This method mutates the array and returns a reference to the same array.

() => unknown
required

Removes the first element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.

(start?: number, end?: number) => unknown[]
required

Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

(compareFn?: (a: unknown, b: unknown) => number) =>
required

Sorts an array in place. This method mutates the array and returns a reference to the same array.

{ (start: number, deleteCount?: number): unknown[]; (start: number, deleteCount: number, ...items: unknown[]): unknown[]; }
required

Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.

(...items: unknown[]) => number
required

Inserts new elements at the start of an array, and returns the new length of the array.

(searchElement: unknown, fromIndex?: number) => number
required

Returns the index of the first occurrence of a value in an array, or -1 if it is not present.

(searchElement: unknown, fromIndex?: number) => number
required

Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present.

{ <S extends unknown>(predicate: (value: unknown, index: number, array: unknown[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: unknown, index: number, array:
required

Determines whether all the members of an array satisfy the specified test.

(predicate: (value: unknown, index: number, array: unknown[]) => unknown, thisArg?: any) => boolean
required

Determines whether the specified callback function returns true for any element of an array.

(callbackfn: (value: unknown, index: number, array: unknown[]) => void, thisArg?: any) => void
required

Performs the specified action for each element in an array.

<U>(callbackfn: (value: unknown, index: number, array: unknown[]) => U, thisArg?: any) => U[]
required

Calls a defined callback function on each element of an array, and returns an array that contains the results.

{ <S extends unknown>(predicate: (value: unknown, index: number, array: unknown[]) => value is S, thisArg?: any): S[]; (predicate: (value: unknown, index: number, array: unknown[]) => unknown,
required

Returns the elements of an array that meet the condition specified in a callback function.

{ (callbackfn: (previousValue: unknown, currentValue: unknown, currentIndex: number, array: unknown[]) => unknown): unknown; (callbackfn: (previousValue: unknown, currentValue: unknown, currentIndex: number, array: unknown[]) => unknown, initialValue: unknown): unknown; <
required

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

{ (callbackfn: (previousValue: unknown, currentValue: unknown, currentIndex: number, array: unknown[]) => unknown): unknown; (callbackfn: (previousValue: unknown, currentValue: unknown, currentIndex: number, array: unknown[]) => unknown, initialValue: unknown): unknown; <
required

Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

{ <S extends unknown>(predicate: (this: void, value: unknown, index: number, obj: unknown[]) => value is S, thisArg?: any): S; (predicate: (value: unknown, index: number, obj:
required

Returns the value of the first element in the array where predicate is true, and undefined otherwise.

(predicate: (value: unknown, index: number, obj: unknown[]) => unknown, thisArg?: any) => number
required

Returns the index of the first element in the array where predicate is true, and -1 otherwise.

(value: unknown, start?: number, end?: number) =>
required

Changes all array elements from start to end index to a static value and returns the modified array

(target: number, start: number, end?: number) =>
required

Returns the this object after copying a section of the array identified by start and end to the same array starting at position target

() => IterableIterator<[number, unknown]>
required

Returns an iterable of key, value pairs for every entry in the array

() => IterableIterator<number>
required

Returns an iterable of keys in the array

() => IterableIterator<unknown>
required

Returns an iterable of values in the array

(searchElement: unknown, fromIndex?: number) => boolean
required

Determines whether an array includes a certain element, returning true or false as appropriate.

<U, This = undefined>(callback: (this: This, value: unknown, index: number, array: unknown[]) => U | readonly U[], thisArg?: This) => U[]
required

Calls a defined callback function on each element of an array. Then, flattens the result into a new array. This is identical to a map followed by flat with depth 1.

<A, D extends number = 1>(this: A, depth?: D) => FlatArray<A, D>[]
required

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

() => IterableIterator<unknown>
required

Iterator

() => { copyWithin: boolean; entries: boolean; fill: boolean; find: boolean; findIndex: boolean; keys: boolean; values: boolean; }
required

Returns an object whose properties have the value 'true' when they will be absent when used in a 'with' statement.

(index: number) => unknown
required

Takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

Was this section helpful?

Example code

import {flattenConnection} from '@shopify/hydrogen-react';

export function ProductList({productConnection}) {
const products = flattenConnection(productConnection);
return (
<ul>
{products.map((product) => (
<li key={product.id}>{product.title}</li>
))}
</ul>
);
}